Search code examples
iosobjective-cnsdata

Getting Memory Leakage - NSData conversion


I am getting memory pressure warning for my iPhone 4 with following scenario: I need to upload my image to server and am using Amazon s3. I am converting my selected image into NSMutableData with following code:

CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);

CFDictionaryRef options = (__bridge CFDictionaryRef)@{(id)kCGImageSourceCreateThumbnailWithTransform: (id)kCFBooleanTrue,
                                                      (id)kCGImageSourceCreateThumbnailFromImageIfAbsent: (id)kCFBooleanTrue,
                                                      (id)kCGImageSourceThumbnailMaxPixelSize: [NSNumber numberWithDouble: maxSize],
                                                      (id)kCGImageDestinationLossyCompressionQuality: [NSNumber numberWithDouble:compressionQuality]};

CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(source, 0, options); // Create scaled image

CFStringRef UTI = kUTTypeJPEG;
NSMutableData *destData = [NSMutableData data];
CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)destData, UTI, 1, NULL);
if (!destination) {
    NSLog(@"Failed to create image destination");
}
CGImageDestinationAddImage(destination, thumbnail,( __bridge CFDictionaryRef)metadata); // copy all metadata in source to destination
if (!CGImageDestinationFinalize(destination)) {
    NSLog(@"Failed to create data from image destination");
}

if (!destination) CFRelease(destination);
CFRelease(source);
CFRelease(thumbnail);

return [destData copy];

Why am I getting a memory warning after 24th image when I execute this in loop?


Solution

  • As you are loading this image in memory as a whole , so its fetching a memory problem. Normally i would save the image to Document directory and then upload the image by multipart using the PATH of the image. That would only load the image as multipart hence memory issue would not be an issue. I dont know if this would be applicable for the thing you are doing.

    Please let me know.