Most memory efficient way to save a photo to disk on iPhone?

前端 未结 3 622
耶瑟儿~
耶瑟儿~ 2020-12-28 18:31

From profiling with Instruments I have learned that the way I am saving images to disk is resulting in memory spikes to ~60MB. This results in the

相关标签:
3条回答
  • 2020-12-28 18:47

    Try to use NSAutoReleasePool and drain the pool once u finish writing the data.

    0 讨论(0)
  • 2020-12-28 19:01

    Using UIImageJPEGRepresentation requires that you have the original and final image in memory at the same time. It may also cache the fully rendered image for a while, which would use a lot of memory.

    You could try using a CGImageDestination. I do not know how memory efficient it is, but it has the potential to stream the image directly to disk.

    +(void) writeImage:(UIImage *)inImage toURL:(NSURL *)inURL withQuality:(double)inQuality {
        CGImageDestinationRef destination = CGImageDestinationCreateWithURL( (CFURLRef)inURL , kUTTypeJPEG , 1 , NULL );
        CFDictionaryRef properties = (CFDictionaryRef)[NSDictionary dictionaryWithObject:[NSNumber numberWithDouble:inQuality] forKey:kCGImageDestinationLossyCompressionQuality];
        CGImageDestinationAddImage( destination , [inImage CGImage] , properties );
        CGImageDestinationFinalize( destination );
        CFRelease( destination );
    }
    
    0 讨论(0)
  • 2020-12-28 19:05

    Are your images actually 60MB compressed, each? If they are, there's not a lot you can do if you want to save them as a single JPEG file. You can try rendering them down to smaller images, or tile them and save them to separate files.

    I don't expect your ImageIO code snippet to improve anything. If there were a two-line fix, then UIImageJPEGRepresentation would be using it internally.

    But I'm betting that you don't get 60MB from a single image. I'm betting you get 60MB from multiple images saved in a loop. And if that's the case, then there is likely something you can do. Put an @autoreleasepool{} inside your loop. It is quite possible that you're accumulating autoreleased objects, and that's leading to the spike. Adding a pool inside your loop allows it to drain.

    0 讨论(0)
提交回复
热议问题