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 554721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:44:24+00:00 2026-05-13T11:44:24+00:00

I need to paint an image using some data and show it on my

  • 0

I need to paint an image using some data and show it on my iphone application. Since the painting takes significant time (2-3 seconds on device), I want to perform painting on a different thread. Also, I want to be able to cancel painting, change something in data and start it again. So it’s best for me to use NSOperation.

Now, when I do the drawing on the main thread, everything looks fine.
When I do exactly the same thing using NSOperation subclass, everything looks fine, but only 95% of the time. Sometimes it doesnt draw the full picture. Sometimes it doesnt draw text. Sometimes it uses different colors, there might be red/green/blue dots scattered all over the image etc etc etc.


I made a very short example to illustrate this:
First, we do all the painting on a main thread in a regular method:

//setting up bitmap context
 size_t width = 400;
 size_t height = 400;
 size_t bitsPerComponent = 8;
 size_t bytesPerRow = 4 * width;
 void* imageData = malloc(bytesPerRow * height);
 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
 CGContextRef context = CGBitmapContextCreate(imageData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);
 CFRelease(colorSpace);

 //transforming it to usual coordinate system
 CGRect mapRect = CGRectMake(0, 0, width, height);
 UIGraphicsPushContext(context);
 CGContextTranslateCTM(context, 0, mapRect.size.height);
 CGContextScaleCTM(context, 1, -1);

 //actull drawing - nothing complicated here, 2 lines and 3 text strings on white background
 CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
 CGContextFillRect(context, mapRect);

 CGContextSetLineWidth(context, 3);
 CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);

 CGContextMoveToPoint(context, 10, 10);
 CGContextAddLineToPoint(context, 20, 20);
 CGContextStrokePath(context);

 CGContextMoveToPoint(context, 20, 20);
 CGContextAddLineToPoint(context, 100, 100);
 CGContextStrokePath(context);

 [UIColor blackColor].set;
 [[NSString stringWithString:@"tag1"] drawInRect:CGRectMake(10, 10, 40, 15) withFont:[UIFont systemFontOfSize:15]];
 [[NSString stringWithString:@"tag2"] drawInRect:CGRectMake(20, 20, 40, 15) withFont:[UIFont systemFontOfSize:15]];
 [[NSString stringWithString:@"tag3"] drawInRect:CGRectMake(100, 100, 40, 15) withFont:[UIFont systemFontOfSize:15]];

 //getting UIImage from bitmap context
 CGImageRef _trueMap = CGBitmapContextCreateImage(context);
 if (_trueMap) {
  UIImage* _map = [UIImage imageWithCGImage:_trueMap];
  CFRelease(_trueMap);
  //displaying what we got
  //self.map leads to UIImageView
  self.map = _map;
 }

 //releasing context and memmory
 UIGraphicsPopContext();
 CFRelease(context);
 free(imageData);

No errors here. Always works.


Now, I’ll subclass NSOperation and copy-paste this code there: Interface:

@interface Painter : NSOperation {
 //The controller which contains UIImageView we will use to display image
 MapViewController* mapViewController;

 CGContextRef context;
 void* imageData;
}

@property (nonatomic, assign) MapViewController* mapViewController;

- (id) initWithRootController:(MapViewController*)mvc__;

@end

Now the methods:

- (id) initWithRootController:(MapViewController*)mvc__ {
 if (self = [super init]) { 
  self.mapViewController = mvc__;
  size_t width = 400;
  size_t height = 400;
  size_t bitsPerComponent = 8;
  size_t bytesPerRow = 4 * width;
  imageData = malloc(bytesPerRow * height);
  CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
  context = CGBitmapContextCreate(imageData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);
  CFRelease(colorSpace);
 }
 return self;
}
- (void) main {
 size_t width = 400;
 size_t height = 400;

 //transforming it to usual coordinate system
 CGRect mapRect = CGRectMake(0, 0, width, height);
 UIGraphicsPushContext(context);
 CGContextTranslateCTM(context, 0, mapRect.size.height);
 CGContextScaleCTM(context, 1, -1);

 //actull drawing - nothing complicated here, 2 lines and 3 text strings on white background
 CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
 CGContextFillRect(context, mapRect);

 CGContextSetLineWidth(context, 3);
 CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);

 CGContextMoveToPoint(context, 10, 10);
 CGContextAddLineToPoint(context, 20, 20);
 CGContextStrokePath(context);

 CGContextMoveToPoint(context, 20, 20);
 CGContextAddLineToPoint(context, 100, 100);
 CGContextStrokePath(context);

 [UIColor blackColor].set;
 [[NSString stringWithString:@"tag1"] drawInRect:CGRectMake(10, 10, 40, 15) withFont:[UIFont systemFontOfSize:15]];
 [[NSString stringWithString:@"tag2"] drawInRect:CGRectMake(20, 20, 40, 15) withFont:[UIFont systemFontOfSize:15]];
 [[NSString stringWithString:@"tag3"] drawInRect:CGRectMake(100, 100, 40, 15) withFont:[UIFont systemFontOfSize:15]];

 //getting UIImage from bitmap context
 CGImageRef _trueMap = CGBitmapContextCreateImage(context);
 if (_trueMap) {
  UIImage* _map = [UIImage imageWithCGImage:_trueMap];
  CFRelease(_trueMap);
  //displaying what we got
  [mapViewController performSelectorOnMainThread:@selector(setMap:) withObject:_map waitUntilDone:YES];
 }

 //releasing context and memmory
 UIGraphicsPopContext();
 CFRelease(context);
 free(imageData);
}

Again, no significant code changes between this 2 pieces of code. And when I start this operation like this:

NSOperationQueue* repaintQueue = [[NSOperationQueue alloc] init];
repaintQueue.maxConcurrentOperationCount = 1;
[repaintQueue addOperation:[[[Painter alloc] initWithRootController:self] autorelease]];

It will work. But not always, sometimes image will contain artifacts.


I’ve also made few screenshots to illustrate the issue, but couldn’t post them =(
Anyways, there is a screenshot which shows red line and 3 text lines (which is fine) and a screenshot which shows red line, no text lines and “tag2” written upside down on tab bar controller.

So what’s the problem?
I cant use Quartz with NSOperation? Is there some kind of restriction on drawing on separate threads? Is there a way to bypass those restrictions if so?
If anyone ever seen this problem, please reply.

  • 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-05-13T11:44:24+00:00Added an answer on May 13, 2026 at 11:44 am

    A few calls in your code are form UIKit (the ones prefixed UI), and UIKit is not threadsafe. Any UI operations should be called on the main thread, or you risk weird things happening, such as artefacting.

    I can’t speak for Quartz2d (or Core Grahics) itself, as I haven’t used a lot of it directly. But I do know that UIKit is not threadsafe.

    The method drawInRect:withFont: is from a category added to NSString from UIKit (UIStringDrawing). These methods are not threadsafe, and that is why you are seeing strange behaviour.

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

Sidebar

Related Questions

I have a paint application that runs as a Java applet. I need to
I need to print out data into a pre-printed A6 form (1/4 the size
From a logical point of view an application may need dozens or hundreds of
I'm developing a shareware desktop application. I'm to the point where I need to
Need a function that takes a character as a parameter and returns true if
I need to develop a file indexing application in python and wanted to know
i have a input tag which is non editable, but some times i need
I have a DateTime object that I need to print in a custom gridlike
I need to write a unit test for a method that will print a
I recently had a need to interpret a DEC 32-bit floating point representation. It

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.