iEngineer
iEngineer

Reputation: 1319

UIImageJPEGRepresentation taking huge memory

i'm trying to figure out this problem but failed after doing every thing i found on OS or google. Problem is that when i convert UIImage to NSData using UIImageJPEGRepresentation or UIImagePNGRepresentation it increases the memory size to 30Mb (believe me or not).
Here is my code

myImage= image;
LoginSinglton*loginObj = [LoginSinglton sharedInstance];
NSError *error;
NSData *pngData = UIImageJPEGRepresentation(image, scaleValue); //scaleVale is 1.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory

self.imageCurrentDateAndTime =[self getTimeAndDate];
 self.filePathAndDirectory = [documentsPath stringByAppendingPathComponent:@"Photos Dir"];

NSLog(@"Documents path %@",self.filePathAndDirectory);
if (![[NSFileManager defaultManager] createDirectoryAtPath:self.filePathAndDirectory
                               withIntermediateDirectories:NO
                                                attributes:nil
                                                     error:&error])
{
    NSLog(@"Create directory error: %@", error);
}
self.imageName= [NSString stringWithFormat:@"photo-%@-%@.jpg",loginObj.userWebID,self.imageCurrentDateAndTime];
 NSString *filePath = [self.filePathAndDirectory stringByAppendingPathComponent:self.imageName];

[pngData writeToFile:filePath atomically:YES]; //Write the file
[self writeImageThumbnailToFolder:image];
[self writeImageHomeViewThumbnailToFolder:image];  

I have tried following solution as well UIImageJPEGRepresentation - memory release issue
1- Used @autoreleasepool
2- done pngData = nil;
but still facing that memory issue.

EDIT I think i'm not able to convey my problem. It's ok if UIImageJPEGRepresentation taking huge memory,but memory should back to it's earlier position after saving that image. Hope this will help you in detail.

Upvotes: 4

Views: 6796

Answers (3)

zaph
zaph

Reputation: 112857

To resize using minimum memory try Using CoreGraphics

SO answer by @Mina Nabil, see full answer for more details

#import <ImageIO/ImageIO.h>
-(UIImage*) resizedImageToRect:(CGRect) thumbRect {
    CGImageRef          imageRef = [inImage CGImage];
    CGImageAlphaInfo    alphaInfo = CGImageGetAlphaInfo(imageRef);
    if (alphaInfo == kCGImageAlphaNone)
        alphaInfo = kCGImageAlphaNoneSkipLast;

    // Build a bitmap context that's the size of the thumbRect
    CGContextRef bitmap = CGBitmapContextCreate(
        NULL,
        thumbRect.size.width,       // width
        thumbRect.size.height,      // height
        CGImageGetBitsPerComponent(imageRef),   // really needs to always be 8
        4 * thumbRect.size.width,   // rowbytes
        CGImageGetColorSpace(imageRef),
        alphaInfo
        );

    // Draw into the context, this scales the image
    CGContextDrawImage(bitmap, thumbRect, imageRef);

    // Get an image from the context and a UIImage
    CGImageRef  ref = CGBitmapContextCreateImage(bitmap);
    UIImage*    result = [UIImage imageWithCGImage:ref];

    CGContextRelease(bitmap);   // ok if NULL
    CGImageRelease(ref);

    return result;
}

Upvotes: 0

Rajesh Loganathan
Rajesh Loganathan

Reputation: 11217

Try this:

UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

If not got solution for your expectation, no need to worry try this too:

UIImage *small = [UIImage imageWithCGImage:original.CGImage scale:0.25 orientation:original.imageOrientation];

Get sample app form here for editing image or scale: http://mattgemmell.com/2010/07/05/mgimageutilities/

Upvotes: 0

Guy Kogus
Guy Kogus

Reputation: 7341

Use a scaleValue of less than 1. Even 0.9 will massively reduce the memory footprint with minimal quality loss.

Upvotes: 8

Related Questions