我正试图解决这个问题,但在做了我在操作系统或谷歌上发现的每一件事后都失败了。问题是,当我使用UIImage或UIImagePNGRepresentation将NSData转换为UIImageJPEGRepresentation时,内存大小会增加到30 me (信不信由你)。
这是我的密码
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占用了巨大的内存,就可以了,但是内存应该在保存该映像后恢复到以前的位置。希望这会对你的细节有所帮助。
发布于 2013-11-27 14:15:56
使用小于1的scaleValue。即使是0.9也会大大减少内存占用,并且质量损失最小。
发布于 2013-11-27 15:21:25
试试这个:
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/
发布于 2013-11-27 16:58:59
若要使用最小内存调整大小,请使用CoreGraphics
因此,answer by @Mina,详见完整答案。
#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;
}https://stackoverflow.com/questions/20244782
复制相似问题