2011-05-20 32 views
-1

我的代碼在這裏。圖像閃爍使用drawInRect方法我在做什麼錯誤

-(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)]; 
} 
+0

如果您希望人們爲回答您的問題付出一些努力,您可以花一些精力來構建您的問題。你在看什麼?你期望看到什麼?你發佈的兩種方法如何被調用? – idz 2011-05-20 07:17:05

+0

好,謝謝你的建議。 – Nitin 2011-05-20 07:22:04

+0

有了更多信息,您可能會得到一個很好的答案,但我們確實需要更多信息。 – idz 2011-05-20 07:25:32

回答

0

這是我的建議:改用UIImageView代替。你不應該在每次發生drawRect:時畫圖。圖像加載和繪製由系統控制很好,所以你需要的是將UIImage對象設置爲UIImageView圖像屬性。如果你檢查UIImageView的文檔,你會發現它根本不調用drawRect:方法。

+0

Thanx爲您的建議,但我需要使用drawRect:方法。 – Nitin 2011-05-20 07:21:00

+0

@Nitin:我只是想知道,爲什麼? – Zapko 2011-05-20 07:42:06