如何在接收方法中快速释放内存?

时间:2013-12-17 22:15:37

标签: ios objective-c memory image-processing uiimage

在我的iPhone应用程序中,我有一个大的图像,我已经缓存到磁盘,我在将图像传递给对该图像进行大量处理的类之前检索它。接收类只需要对图像进行一些初始化,我想尽快释放图像占用的内存,因为图像处理代码非常耗费内存,但我不知道如何。

它看起来像这样:

// inside viewController
- (void) pressedRender
{
    UIImage *imageToProcess = [[EGOCache globalCache] imageForKey:@"reallyBigImage"];
    UIImage *finalImage = [frameBuffer renderImage:imageToProcess]; 
    // save the image
}


// inside frameBuffer class
- (UIImage *)renderImage:(UIImage *)startingImage
{
    CGContextRef context = CGBitmapCreateContext(....)
    CGContextDrawImage(context, rect, startingImage.CGImage);

    // at this point, I no longer need the image 
    // and would like to release the memory it's taking up

    // lots of image processing/memory usage here...


    // return the processed image
    CGImageRef tmpImage = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    UIImage *renderedImage = [UIImage imageWithCGImage:tmpImage];
    CGImageRelease(tmpImage);
    return renderedImage;
}

这可能是显而易见的,但我遗漏了一些东西。谢谢。

2 个答案:

答案 0 :(得分:2)

@ Jonah.at.GoDaddy走在正确的轨道上,但我会更明确地做出这一切,而不是依靠ARC优化。 ARC在调试模式下的攻击性要小得多,因此除非您采取措施,否则在调试时内存使用率可能会过高。

UIImage *imageToProcess = [[EGOCache globalCache] imageForKey:@"reallyBigImage"];

首先,我将假设imageForKey:本身不缓存任何内容,并且不会调用imageNamed:(缓存内容)。

关键是当你想让内存消失时你需要弄乱你的指针。如果你将图像从一个地方传递到另一个地方(乔纳的解决方案也修复了),这将非常困难。就个人而言,我可能会做这样的事情,尽可能快地从image->上下文中获取:

CGContextRef CreateContextForImage(UIImage *image) {
    CGContextRef context = CGBitmapCreateContext(....)
    CGContextDrawImage(context, rect, image.CGImage);
    return context;
}

- (void) pressedRender {

    CGContextRef context = NULL;

    // I'm adding an @autoreleasepool here just in case there are some extra 
    // autoreleases attached by imageForKey: (which it's free to do). It also nicely
    // bounds the references to imageToProcess.
    @autoreleasepool {      
      UIImage *imageToProcess = [[EGOCache globalCache] imageForKey:@"reallyBigImage"];
      context = CreateContextForImage(imageToProcess);
    }
    // The image should be gone now; there is no reference to it in scope.

    UIImage *finalImage = [frameBuffer renderImageForContext:context]; 
    CGContextRelease(context);
    // save the image
}


// inside frameBuffer class
- (UIImage *)renderImageForContext:(CGContextRef)context
{
    // lots of memory usage here...
     return renderedImage;
}

对于调试,您可以通过向其添加关联的观察程序来确保UIImage真正消失。请参阅How to enforce using `-retainCount` method and `-dealloc` selector under ARC?的已接受答案(答案与问题几乎没有关系;它恰好可以解决您可能会发现有用的问题)。

答案 1 :(得分:2)

您可以使用相同的方法立即自动释放对象。我认为您需要尝试在一种方法中处理“大图像”过程以使用@autorelease:

-(void)myMethod{

//do something

@autoreleasepool{
 // do your heavy image processing and free the memory right away
}

//do something
}