开发者

Objective c: drawing a png on the screen (UIImage inside a UIImageView)

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;

开发者_Python百科    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


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.
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜