Problem between context and stream in drawRect

I am trying to draw a couple UIImagein UIView, and I am doing it manually using drawRect:. Given that the images are dynamically downloaded from the Internet, I create NSOperationand I execute the code for loading the image from the second stream to support the user interface. Therefore, as soon as the images are downloaded, they are displayed on the screen.

But ... I get the following errors for each image:

<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextSetBlendMode: invalid context 0x0
<Error>: CGContextSetAlpha: invalid context 0x0
<Error>: CGContextTranslateCTM: invalid context 0x0
<Error>: CGContextScaleCTM: invalid context 0x0
<Error>: CGContextDrawImage: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0

For what I've seen, this means that the context in which I'm trying to do [image drawInRect...], nilbecause I am no longer in drawRect:.

Attempt to do

UIGraphicsPushContext(UIGraphicsGetCurrentContext());

before drawing the image, but nothing has changed. How can I overcome this? Multithreading is a must to react, and context is lost on the way.

UPDATE: :

- (void)drawContentView:(CGRect)r
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    ....
    CGContextFillRect(context, r);
    UIApplication* app = [UIApplication sharedApplication];
        app.networkActivityIndicatorVisible = YES;


    NSOperationQueue *queue = [NSOperationQueue new];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc]
                                            initWithTarget:self 
                                            selector:@selector(loadPreview) 
                                            object:nil];
    [queue addOperation:operation];
    [operation release];
}

-(void)loadPreview
{
    self.preview = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:self.picStringPreview]]];
    [self.preview drawInRect:CGRectMake(256, 18, 80, 65)];
}

UIGraphicsPushContext... loadPreview, [self performSelectorOnMainThread...], . , Atebit, :

- (void)drawRect:(CGRect)r
{
    [(ABTableViewCell *)[self superview] drawContentView:r];
}

drawContentView: draw'd drawRect:. ?

+3
2

, . UIKit , drawRect:, . , , , draw..., framework.

? . , . , , draw drawContentView:, , . loadPreview setNeedsDisplay *, drawContentView: , preview nil, . Anomie, :

-(void)loadPreview
{
    self.preview = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:self.picStringPreview]]];
    [self setNeedsDisplay];
}

- (void)drawContentView:(CGRect)r
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    ....
    CGContextFillRect(context, r);
    UIImage * prvw = self.preview;    // Avoid making two method calls
    if( prvw ){
        [prvw drawInRect:CGRectMake(256, 18, 80, 65)];
    }
    // etc.

, , NSOperation . , , .


* , Anomie , performSelector:...onMainThread:, setNeedsDisplay drawRect: , .

+3

- drawRect: , UIImageViews. Apple , drawRect: - , .

, ivars ( ), setNeedsDisplay setNeedsDisplayInRect: ( , performSelector:withObject:onMainThread:, ), , drawRect:, , drawRect: , .

+2

All Articles