Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用过多内存减少图像尺寸。。。有没有一种方法可以使用更少的内存?_Ios_Iphone_Ipad_Cocoa Touch - Fatal编程技术网

Ios 使用过多内存减少图像尺寸。。。有没有一种方法可以使用更少的内存?

Ios 使用过多内存减少图像尺寸。。。有没有一种方法可以使用更少的内存?,ios,iphone,ipad,cocoa-touch,Ios,Iphone,Ipad,Cocoa Touch,我有一个使用相机拍照的应用程序。照片一拍下来,我就缩小相机拍摄的图像的大小 运行减少图像大小的方法,使内存使用峰值从21MB增加到61MB,有时接近69MB 我已经将@autoreleasepool添加到这个过程中涉及的每个方法中。情况有所改善,但没有我预期的那么多。我不希望在缩小图像时内存使用量会增加3倍,特别是因为生成的新图像更小 以下是我尝试过的方法: - (UIImage*)reduceImage:(UIImage *)image toSize:(CGSize)size { @

我有一个使用相机拍照的应用程序。照片一拍下来,我就缩小相机拍摄的图像的大小

运行减少图像大小的方法,使内存使用峰值从21MB增加到61MB,有时接近69MB

我已经将@autoreleasepool添加到这个过程中涉及的每个方法中。情况有所改善,但没有我预期的那么多。我不希望在缩小图像时内存使用量会增加3倍,特别是因为生成的新图像更小

以下是我尝试过的方法:

- (UIImage*)reduceImage:(UIImage *)image toSize:(CGSize)size {

    @autoreleasepool {
        UIGraphicsBeginImageContext(size);

        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextTranslateCTM(context, 0.0, size.height);
        CGContextScaleCTM(context, 1.0, -1.0);

        CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, size.width, size.height), image.CGImage);

        UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        return scaledImage;

    }
}
而且

- (UIImage *)reduceImage:(UIImage *)image toSize:(CGSize)size {

    @autoreleasepool {
        UIGraphicsBeginImageContext(size);

        [image drawInRect:rect];
        UIImage * result =  UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        return result;
    }
}
这两者之间没有任何区别

注意:原始图像为3264x2448像素x 4字节/像素=32MB,最终图像为1136x640,即2.9MB。。。将两个数字相加,得到35MB,而不是70

有没有一种方法可以在不使内存使用峰值达到平流层的情况下减小图像的大小?谢谢

顺便说一句,出于好奇:有没有一种方法可以在不使用石英的情况下降低图像尺寸?

答案是

使用CoreGraphics,节省30~40%的内存

    #import <ImageIO/ImageIO.h>
   -(UIImage*) resizedImageToRect:(CGRect) thumbRect
{
    CGImageRef          imageRef = [inImage CGImage];
    CGImageAlphaInfo    alphaInfo = CGImageGetAlphaInfo(imageRef);

    // There's a wierdness with kCGImageAlphaNone and CGBitmapContextCreate
    // see Supported Pixel Formats in the Quartz 2D Programming Guide
    // Creating a Bitmap Graphics Context section
    // only RGB 8 bit images with alpha of kCGImageAlphaNoneSkipFirst, kCGImageAlphaNoneSkipLast, kCGImageAlphaPremultipliedFirst,
    // and kCGImageAlphaPremultipliedLast, with a few other oddball image kinds are supported
    // The images on input here are likely to be png or jpeg files
    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;
}
#导入
-(UIImage*)resizedImageToRect:(CGRect)thumbRect
{
CGImageRef imageRef=[inImage CGImage];
CGImageAlphaInfo alphaInfo=CGImageGetAlphaInfo(imageRef);
//KCGIMAGEALPHONE和CGBitmapContextCreate有一种wierdness
//请参阅《Quartz 2D编程指南》中支持的像素格式
//创建位图图形上下文部分
//仅包含KCGIMAGEALPHANEONSKIPFIRST、KCGIMAGEALPHANEONSKIPLAST、KCGIMAGEALPHANEONSKIPFIRST、KCGIMAGEALPHANEONSJIEDFIRST、,
//和kCGImageAlphaPremultipliedLast,以及其他一些古怪的图像类型都受支持
//此处输入的图像可能是png或jpeg文件
如果(alphaInfo==kCGImageAlphaNone)
alphaInfo=kCGImageAlphaNoneSkipLast;
//构建一个与thumbRect大小相同的位图上下文
CGContextRef位图=CGBitmapContextCreate(
无效的
thumbRect.size.width,//宽度
thumbRect.size.height,//高度
CGImageGetBitsPerComponent(imageRef),//确实需要始终为8
4*thumbRect.size.width,//行字节
CGImageGetColorSpace(imageRef),
alphaInfo
);
//在上下文中绘制,这将缩放图像
CGContextDrawImage(位图、thumbRect、imageRef);
//从上下文和UIImage中获取图像
CGImageRef ref=CGBitmapContextCreateImage(位图);
UIImage*结果=[UIImage imageWithCGImage:ref];
CGContextRelease(位图);//如果为NULL,则确定
CGImageRelease(ref);
返回结果;
}

作为一个类别添加到UIImage。

我注意到,当您处理图像时,由于ios7的内存消耗比以前高得多。还有一些人在苹果开发者论坛上对此抱怨,但没有解决方案。我相信,这似乎是合理的,当绘制一幅图像时,它最初是压缩的,而不是编码的,在一个上下文中,将解压缩并解码图像到“原始像素缓冲区”。这必然需要内存。现在的问题是,如何去掉包含像素数据的内部缓冲区,除非绘制图像,否则不需要这些数据。当然,在释放UIImage对象之前,需要将缩放图像写入磁盘。好的,但原始图像是3264 x 2448像素x 4字节/像素=32MB,最终图像是1136x640,即2.9MB。。。将两个数字相加,得到35MB,而不是70!UIImage缓存内部缓冲区。只有在内存不足的情况下,系统才可能决定“清除”缓存的缓冲区。这在“引擎盖下”透明地发生。我不知道如何控制
UIImage
的这种行为
NSImage
有一个
recache
方法。@peko完全正确。记忆不是问题吗?