UIImageJPEG huge memory development

I am trying to find out this problem, but could not complete everything I found on the OS or Google. The problem is that when converting UIImageto NSDatausing UIImageJPEGRepresentationeither UIImagePNGRepresentationit increases the memory size to 30 MB (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 also tried the following solution UIImageJPEGRepresentation - memory release problem
1- Used @autoreleasepool
2- done pngData = nil;
but still facing this memory issue.

EDIT , . , UIImageJPEGRepresentation , , . , .

+4
3

scaleValue 1. 0.9 .

+7

:

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

, , :

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

: http://mattgemmell.com/2010/07/05/mgimageutilities/

0

To resize using minimal memory, try using CoreGraphics

SO answer from @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 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;
}
0
source

All Articles