首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIImageJPEGRepresentation占用巨大内存

UIImageJPEGRepresentation占用巨大内存
EN

Stack Overflow用户
提问于 2013-11-27 14:14:36
回答 3查看 6.7K关注 0票数 4

我正试图解决这个问题,但在做了我在操作系统或谷歌上发现的每一件事后都失败了。问题是,当我使用UIImageUIImagePNGRepresentationNSData转换为UIImageJPEGRepresentation时,内存大小会增加到30 me (信不信由你)。

这是我的密码

代码语言:javascript
复制
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- Used @autoreleasepool

2- done pngData =0;

但仍然面临着记忆问题。

编辑我想我无法表达我的问题。如果UIImageJPEGRepresentation占用了巨大的内存,就可以了,但是内存应该在保存该映像后恢复到以前的位置。希望这会对你的细节有所帮助。

EN

回答 3

Stack Overflow用户

发布于 2013-11-27 14:15:56

使用小于1的scaleValue。即使是0.9也会大大减少内存占用,并且质量损失最小。

票数 8
EN

Stack Overflow用户

发布于 2013-11-27 15:21:25

试试这个:

代码语言:javascript
复制
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

如果您的期望得不到解决方案,也不必担心这一点:

代码语言:javascript
复制
UIImage *small = [UIImage imageWithCGImage:original.CGImage scale:0.25 orientation:original.imageOrientation];

在这里获取用于编辑图像或缩放的示例应用程序表单:http://mattgemmell.com/2010/07/05/mgimageutilities/

票数 0
EN

Stack Overflow用户

发布于 2013-11-27 16:58:59

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

因此,answer by @Mina,详见完整答案。

代码语言:javascript
复制
#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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20244782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档