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

前端 未结 3 634
耶瑟儿~
耶瑟儿~ 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 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 );
    }
    

提交回复
热议问题