gpt4 book ai didi

ios - 与 assetsd 的连接中断或 assetsd 死亡(带有内存警告)

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:58:42 25 4
gpt4 key购买 nike

我们正在尝试让用户从他们的相册中导入图片(UIImagePickerController),并且我们正在缩放/调整大于 8 兆像素(iPhone 标准)的图像。

但每次应用程序崩溃时,与 assetsd 的连接中断或 assetsd 死亡Received memory warning 在导入图片之后或之前发出警告。有时 Received memory warning 仍在UIImagePickerController中寻找要导入的图片时弹出警告。

特别是在 iPhone 4S 上,情况更糟,请帮助我们优化我们的代码,使其在 iPhone 4S 或 iPad 2 等旧设备上运行时没有警告和崩溃。

如果我们在使用 CoreGraphics 缩小/调整图像大小时做错了什么,请告诉我们。(因为这是使用大量内存的地方)。

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *selectedImage=[info objectForKey:UIImagePickerControllerOriginalImage];
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
else
{
[popoverController dismissPopoverAnimated:YES];
[self popoverControllerDidDismissPopover:popoverController];
}

// COMPRESSING IMAGE
NSData *selectedImageData=UIImageJPEGRepresentation(selectedImage, 0.1);
UIImage *selectedImageFromData=[UIImage imageWithData:selectedImageData];

// IMAGE ASPECT RATIO
CGFloat originalWidth=selectedImageFromData.size.width;
CGFloat originalHeight=selectedImageFromData.size.height;
CGFloat myWidth=2048;
CGFloat myHeight=2048;
CGFloat widthRatio=myWidth/originalWidth;
CGFloat heightRatio=myHeight/originalHeight;
CGFloat dynamicWidth=heightRatio*originalWidth;
CGFloat dynamicHeight=widthRatio*originalHeight;


//SCALING UIIMAGE MORE THAN 8 MEGAPIXELS
if (((selectedImageFromData.size.width>3264) && (selectedImageFromData.size.height>2448)) || ((selectedImageFromData.size.height>3264) && (selectedImageFromData.size.width>2448)))
{



// DATA FROM UIIMAGE TO CORE GRAPHICS
CGImageRef CoreGraphicsImage=selectedImageFromData.CGImage;
CGColorSpaceRef colorSpace = CGImageGetColorSpace(CoreGraphicsImage);
CGBitmapInfo bitmapInfo=CGImageGetBitmapInfo(CoreGraphicsImage);
CGImageGetBitsPerComponent(CoreGraphicsImage);


// RESIZING WIDTH OF THE IMAGE
if (originalWidth>originalHeight)
{


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


//RESIZING HEIGHT OF THE IMAGE
if (originalHeight>originalWidth)
{
CGContextRef context=CGBitmapContextCreate(NULL, dynamicWidth, myHeight, CGImageGetBitsPerComponent(CoreGraphicsImage), CGImageGetBytesPerRow(CoreGraphicsImage), colorSpace, bitmapInfo);


CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextDrawImage(context, CGRectMake(0, 0, dynamicWidth, myHeight), 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;
}


}

内存报告

在 UIImagePickerController 中滚动时

http://i.stack.imgur.com/qxx62.png

缩放/调整 UIImage 大小时

http://i.stack.imgur.com/ELCA6.png

最佳答案

两点。

首先,您没有释放您创建的对象——您正在泄漏大量内存。无论您是否使用 ARC,都必须为每个 CGCreate 调用适本地调用 CGContextRelease 或 CGImageRelease。

其次,如果您只想调整大小,使用 coregraphics 就太过分了,请改用 UIKit。在我下面使用的代码中,请注意 @autorelease 的使用,以确保一旦上下文结束,ARC 就会清除对象

- (UIImage *)fitImage:(UIImage *)image scaledToFillSize:(CGSize)size {
// do not upscale

@autoreleasepool {
if( image.size.width <= size.width && image.size.height <= size.height )
return image;

CGFloat scale = MIN(size.width/image.size.width, size.height/image.size.height);
CGFloat width = image.size.width * scale;
CGFloat height = image.size.height * scale;

CGRect imageRect;
// center image
if( scale != 1.0 ) { // avoid divide by zero?
if( size.width/image.size.width < size.height/image.size.height ) {
// height needs to be centered
imageRect = CGRectMake(0, (size.height-height)/2, width, height);
} else {
// width needs to be centered
imageRect = CGRectMake((size.width-width)/2, 0, width, height);
}
}

UIGraphicsBeginImageContextWithOptions(size, YES, 0);
[[UIColor CollageBorderUIColor] setFill]; // otherwise it's ugly black fill
UIRectFill(CGRectMake(0, 0, size.width, size.height));
[image drawInRect:imageRect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
}

关于ios - 与 assetsd 的连接中断或 assetsd 死亡(带有内存警告),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30137378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com