UIImageJPEGRepresentation占用大量内存

时间:2013-11-27 14:14:36

标签: ios objective-c uiimage

我正在试图找出这个问题,但在做了我在操作系统或谷歌上发现的所有事情后失败了。问题是,当我使用UIImageNSDataUIImageJPEGRepresentation转换为UIImagePNGRepresentation时,它将内存大小增加到30Mb(相信我或不相信)。
这是我的代码

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];  

我也试过以下解决方案 UIImageJPEGRepresentation - memory release issue
1-使用@autoreleasepool
2-完成pngData = nil;
但仍面临着记忆问题。

编辑我想我无法传达我的问题。如果UIImageJPEGRepresentation占用大量内存,内存应该在保存该图像后回到它的早期位置,这是可以的。希望这会对你有所帮助。

3 个答案:

答案 0 :(得分:8)

使用小于1的scaleValue。即使0.9也会大幅减少内存占用,同时将质量损失降至最低。

答案 1 :(得分:0)

试试这个:

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/

答案 2 :(得分:0)

要使用最小内存调整大小,请尝试使用CoreGraphics

@Mina Nabil的answer,详情请见完整答案

#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;
}