Objective c:在屏幕上绘制一个png(UIImageView内的UIImage)

发布于 2024-10-16 05:35:48 字数 1093 浏览 10 评论 0原文

我正在编写一个打砖块,并且正在创建一个类,它将在屏幕上绘制所有元素(球、砖块和桨)。

我试图将官方文档所采用的一些代码放在一起:

    -(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;

}

我无法测试这个,因为我的其他课程尚未完成,但它“概念上”正确吗? 否则,您是否有一些示例可以测试将图像 UIImages 绘制到 UIImageView 中并检索,例如 center.x 和 center.y 位置?

谢谢

I'm writing a brickbreaker and I'm creating a class that will draw all elements (balls, bricks and paddle) on the screen.

I tried to put toghether some code taken by the official doc:

    -(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;

}

I can't test this because my others class aren't finished but is it "conceptually" right?
Otherwise do you have some sample I can test to draw images UIImages into UIImageView et retrive, for example center.x and center.y position?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独难免 2024-10-23 05:35:48

几点:

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

A few points:

  • Assuming you are bundling graphics for your images within your app bundle, why not just use [UIImage imagedName: @"Brick.png"] to load images?
  • You are leaking a UIImage and UIImageView, which suggests you may need to revisit basic memory management documentation.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文