Ios 调整uiimage大小时内存使用率高
在rect中调整和绘制UIImage时占用大量内存Ios 调整uiimage大小时内存使用率高,ios,objective-c,iphone,xcode,ios8.1,Ios,Objective C,Iphone,Xcode,Ios8.1,在rect中调整和绘制UIImage时占用大量内存 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *selectedImage=[info objectForKey:UIImagePickerControllerOriginalImage]; //
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *selectedImage=[info objectForKey:UIImagePickerControllerOriginalImage];
// COMPRESSING IMAGE
NSData *selectedImageData=UIImageJPEGRepresentation(selectedImage, 0.5);
UIImage *selectedImageFromData=[UIImage imageWithData:selectedImageData];
//SCALING UIIMAGE
UIImage *scaledImage=[[UIImage alloc]init];
if (((selectedImageFromData.size.width>3264) && (selectedImageFromData.size.height>2448)) || ((selectedImageFromData.size.height>3264) && (selectedImageFromData.size.width>2448)))
{
CGFloat originalWidth=selectedImageFromData.size.width;
CGFloat originalHeight=selectedImageFromData.size.height;
CGFloat myWidth=2048;
CGFloat widthRatio=myWidth/originalWidth;
CGFloat dynamicHeight=widthRatio*originalHeight;
CGImageRef CoreGraphicsImage=selectedImageFromData.CGImage;
CGColorSpaceRef colorSpace = CGImageGetColorSpace(CoreGraphicsImage);
CGBitmapInfo bitmapInfo=CGImageGetBitmapInfo(CoreGraphicsImage);
CGImageGetBitsPerComponent(CoreGraphicsImage);
CGContextRef context=CGBitmapContextCreate(NULL, myWidth, dynamicHeight, CGImageGetBitsPerComponent(CoreGraphicsImage), CGImageGetBytesPerRow(CoreGraphicsImage), colorSpace, bitmapInfo);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextDrawImage(context, CGRectMake(0, 0, myWidth, dynamicHeight), CoreGraphicsImage);
CGImageRef CGscaledImage=CGBitmapContextCreateImage(context);
UIImage *CGLastimage = [UIImage imageWithCGImage: CGscaledImage];
NSLog(@"%f",CGLastimage.size.width);
NSLog(@"%f",CGLastimage.size.height);
VisualEffectImageVIew.image=CGLastimage;
BackgroundImageView.image=CGLastimage;
ForegroundImageView.image=CGLastimage;
}
else
{
NSLog(@" HEIGHT %f",selectedImageFromData.size.height);
NSLog(@" WIDTH %f",selectedImageFromData.size.width);
VisualEffectImageVIew.image=selectedImageFromData;
BackgroundImageView.image=selectedImageFromData;
ForegroundImageView.image=selectedImageFromData;
}
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
else
{
[popoverController dismissPopoverAnimated:YES];
[self popoverControllerDidDismissPopover:popoverController];
}
}
下面是一幅图,在图中你可以看到上面提到的代码占用了太多的内存。想知道为什么它使用了太多的内存,并将感谢任何解决方案
说到图像,我一直是它的忠实粉丝,它可以做任何你想做的事情,并使用GPU而不是CPU,这让你有足够的能力避免因内存压力而崩溃 而且也很不错 在完成项目设置后,还可以帮助您实现所需
希望这对您有所帮助:)您正在使用CPU将数据绘制到一个帧中,我建议您在GPU上下文中使用CGImage进行绘制
- (UIImage *) scaleToSize: (CGSize)size
{
// Scalling selected image to targeted size
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, size.width, size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
CGContextClearRect(context, CGRectMake(0, 0, size.width, size.height));
if(self.imageOrientation == UIImageOrientationRight)
{
CGContextRotateCTM(context, -M_PI_2);
CGContextTranslateCTM(context, -size.height, 0.0f);
CGContextDrawImage(context, CGRectMake(0, 0, size.height, size.width), self.CGImage);
}
else
CGContextDrawImage(context, CGRectMake(0, 0, size.width, size.height), self.CGImage);
CGImageRef scaledImage=CGBitmapContextCreateImage(context);
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
UIImage *image = [UIImage imageWithCGImage: scaledImage];
CGImageRelease(scaledImage);
return image;
}
在UIImage中加载JPEG并不意味着它不必解码图像的每个像素并将其存储在内存中。仅供参考,3200x2400 px=7.68Mpix,以RGB为单位(每像素24位),这将产生184.32Mbit,或约23Mbytes。是的,但这与我的问题有什么关系?在这里,我们试图缩小超过800万像素的图像。而且,我们不仅在应用程序崩溃时存在内存问题。有关更多信息,请查看上面图像中使用大量内存的代码。只是说,调整百万像素图像的大小需要如此多的内存是正常的。正如答案中所述,您肯定应该使用一些优化的库,如GPUImage。然后我们将对此进行研究。有没有办法在故事板中使用GPUImageView?看看我们更新的代码,CoreGraphics速度更快,但内存警告和崩溃现在变得更严重。(内存报告显示现在使用了150 MB).App在调整下一个(第二个)图像的大小时崩溃。我想说的是,它现在变得更糟了。对不起,它不快,它是一样的