Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8447469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:11:02+00:00 2026-06-10T10:11:02+00:00

I’m fetching an image and saving it to temporary directory in a background thread.

  • 0

I’m fetching an image and saving it to temporary directory in a background thread. Before saving, I want to add a little white border to this UIImage. I’m doing this:

    - (UIImage *)imageWithBorder:(UIImage *)image {
    CGSize newSize = CGSizeMake(image.size.width, image.size.height);
    CGRect rect = CGRectMake(0.0, 0.0, image.size.width, image.size.height);

    UIGraphicsBeginImageContextWithOptions(newSize, NO, image.scale); {
        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextBeginTransparencyLayer (context, NULL);
        //Draw
        [image drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];
        CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
        CGContextStrokeRectWithWidth(context, rect, 10);
        CGContextEndTransparencyLayer(context);
    }
    UIImage *result =  UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return result;
}

Everything is okay, after loading that image from a temporary directory I can see border on an image, but in console I’m getting invalid context 0x0 when the code below implements. What’s wrong here?


Update 01

- (UIImage *)imageWithBorder:(UIImage *)image {
    CGSize newSize = CGSizeMake(image.size.width, image.size.height);
    CGRect rect = CGRectMake(0.0, 0.0, image.size.width, image.size.height);

    UIGraphicsBeginImageContextWithOptions(newSize, NO, image.scale); {
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextDrawImage (context, rect, [image CGImage]);
        CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
        CGContextStrokeRectWithWidth(context, rect, 10);
    }
    UIImage *result =  UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return result;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T10:11:02+00:00Added an answer on June 10, 2026 at 10:11 am

    Try changing:

    [image drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];
    

    to

    CGContextDrawImage (context, rect, [image CGImage]);
    

    Also, why use a transparency layer? You have the alpha set to 1, so you could just overwrite the original image, no?

    EDIT: I just ran the same code in my app, on the background, it worked flawlessly:

    - (UIImage *)imageWithBorder:(UIImage *)image
    {
        NSLog(@"Thread %@", [NSThread currentThread]);
    
        CGSize newSize = CGSizeMake(image.size.width, image.size.height);
        CGRect rect = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    
        UIGraphicsBeginImageContextWithOptions(newSize, NO, image.scale);
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        //Draw
        CGContextDrawImage(context, rect, [image CGImage]);
        CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
        CGContextStrokeRectWithWidth(context, rect, 10);
        UIImage *result =  UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
    assert(result);
        return result;
    }
    

    When I do this in viewDidAppear: (but its a small image, maybe the size is relevant?)

    dispatch_async(dispatch_get_global_queue(0, 0), ^{ [self imageWithBorder:[UIImage imageNamed:@"02-redo.png"]] ; } );
    

    this is all I see in the log:

    2012-08-27 13:48:04.679 Searcher[32825:11103] Thread <NSThread: 0x6d30e30>{name = (null), num = 4}
    

    So something else is going on with you code. I’m building with Xcode 4.4.1, deployment target is 5.1

    EDIT: In your block, you cannot refer to “cell” as cell is temporal – with recycling it may not even be visible. So the image name will be routed through a local NSString, then at the end, you need another method that the image can be posted to, along with the cell indexPath. That method will see if that indexPath is in the visibleCells of the table, if so then update the image, if not you want to store the image so it can be used with the tableView next asks for the cell.

    EDIT2: This code:

    dispatch_async(queue, ^{ 
      UIImage *image = [_thumbnailMaster thumbnailForItemWithFilename:cell.label.text];
      dispatch_sync(dispatch_get_main_queue(), ^{ cell.thumbnailView.image = image; }); 
    });
    

    Should be turned into something that does not involve “cell” as cell may go out of scope or be used by another index (assuming recycling). Something like this:

    NSString *text = cell.label.text;
    NSIndexPath *path = ...;
    
    dispatch_async(queue, ^{ 
      UIImage *image = [_thumbnailMaster thumbnailForItemWithFilename:text];  
       dispatch_sync(dispatch_get_main_queue(), ^{ [self updateCellImage:image withPath"path]; }); 
    });
    

    Your method determines if that cell is visible, if so update the image, if not save it so next time you need it you have it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.