目标c:在屏幕上绘制一个png(UIImageView中的UIImage)

时间:2011-02-07 12:25:04

标签: objective-c uiimageview uiimage cgimage

我正在写一个破砖者,我正在创建一个可以在屏幕上绘制所有元素(球,砖和桨)的类。

我试图将官方文档中的一些代码添加到其中:

    -(id) drawWithGC: (CGContextRef) myContext andCGRect: (CGRect) myContextRect andPath: (const char *) filename
{
    CGImageRef image;
    CGDataProviderRef provider;
    CFStringRef path;
    CFURLRef url;

    path = CFStringCreateWithCString (NULL, filename, kCFStringEncodingUTF8);
    url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, NO);
    CFRelease(path);
    provider = CGDataProviderCreateWithURL (url);
    CFRelease (url);
    image = CGImageCreateWithPNGDataProvider (provider, NULL, true, kCGRenderingIntentDefault);
    CGDataProviderRelease (provider);
    CGContextDrawImage (myContext, myContextRect, image);
    CGImageRelease (image);

    UIImage *finalImage = [[UIImage alloc] imageWithCGImage:image];
    UIImageView *finalImageView = [[UIImage alloc] initWithImage:finalImage];

    return finalImageView;

}

我无法测试这个,因为我的其他课程没有完成,但它在“概念上”是对的吗? 否则,您是否有一些样本我可以测试将图像UIImage绘制到UIImageView和retrive中,例如center.x和center.y位置?

由于

1 个答案:

答案 0 :(得分:0)

几点:

  • 假设您要在应用包中捆绑图像,为什么不使用[UIImage imagedName:@“Brick.png”]来加载图片?
  • 您正在泄露UIImage和UIImageView,这表明您可能需要重新访问基本的内存管理文档。
相关问题