开发者

image is blinking using the method drawInRect what am i doing wrong

开发者 https://www.devze.com 2023-03-07 00:38 出处:网络
My code is here. -(void)displayPacketData:(NSData*)decompressedData:(CEZCmdZLIBCompress*)data { Byte* d=[decompressedData bytes];

My code is here.

-(void)displayPacketData:(NSData*)decompressedData:(CEZCmdZLIBCompress*)data
{
        Byte* d=[decompressedData bytes];
    int width=data->iWidth;
    int height=data->iHeight;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  开发者_开发技巧  size_t bitsPerComponent = 8;
    size_t bytesPerPixel    = 4;
    size_t bytesPerRow      = (width * bitsPerComponent * bytesPerPixel + 7) / 8;
    size_t dataSize         = bytesPerRow * height;
    context = CGBitmapContextCreate([decompressedData bytes], width, height, 
                                    bitsPerComponent, 
                                    bytesPerRow, colorSpace, 
                                    kCGImageAlphaNoneSkipFirst  | kCGBitmapByteOrder32Little);
    layer1 = CGLayerCreateWithContext(context,(struct CGSize){ width, height }, NULL);

    if( layer1 == NULL )
    {
        NSLog(@"Layer could not be created");
    }
    layerContext = CGLayerGetContext(layer1);
    CGContextScaleCTM(context, 1, -1);
    CGContextTranslateCTM(context, 0, height);

    //CGColorSpaceRelease(colorSpace);
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
    UIImage *result = [[UIImage imageWithCGImage:imageRef] retain];
    m_image=result;
    x = data->iX;
    y = data->iY;
    h = height;
    w = width;
    //CGImageRelease(imageRef);
    //CGContextRelease(context);
    //free(data);    
    //[result drawInRect:CGRectMake(10.0, 30.0, x, y)];
    //CGContextRef currentContext = UIGraphicsGetCurrentContext();
    //CGContextDrawLayerAtPoint(context,CGPointZero,layer1);
    //CGContextDrawImage(layerContext, CGRectMake(10.0+x, 30.0+y, w, h), result.CGImage);
    //[self performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:NO];
}

- (void)drawRect:(CGRect)rect 
{
  [m_image drawInRect:CGRectMake(10.0+x, 30.0+y, w, h)];
}


Here is my piece of advice: use UIImageView instead. You shouldn't draw image every time drawRect: occurs. Image loading and drawing is controlled by system pretty good, so all you need is to set UIImage object to UIImageView image property. If you check documentation of UIImageView you'll see that it doesn't call drawRect: method at all.

0

精彩评论

暂无评论...
验证码 换一张
取 消