iPhone iOS的图像调整大小,以实现最低内存使用率解决方案

时间:2012-11-10 12:37:08

标签: iphone ios memory resize

我从原始分辨率图像调整图像大小,大约是8MP,我的输出分辨率大约是2MP。    有时,它显示iPhone4S设备的内存警告,我在内存警告中做了一些发布,但它有时会使我的程序崩溃,因为alassetlibrary使用问题。    这是我的问题,我希望在调整fullReoslutionImage的大小时防止内存警告。

我尝试了很多调整图像大小的方法。    我目前的方法是峰值内存使用量增加约44MB。它似乎太大了......对于整个系统来说,它将达到57 MB的峰值......    我尝试了CGImageSourceCreateThumbnailAtIndex,但它引入了比我当前方法更多的内存增加。

这是我的解决方案,但它有时会引入记忆警告。你有其他方法吗?    你可以帮帮我吗?一些想法,     1.其他iOS API?     2.用于JPEG解码器的动态调整大小的本机库?     3. YUV422缩放器而不是RGBA。     4. .....我不知道......

UIImage *image=[self oriImageThumbnailAtIndex:asset];
        CGInterpolationQuality interpolationQuality=kCGInterpolationMedium;            
        CGSize newSize=[self sizeDown:image.size];            
        image2 =makeResizedImageRotate(image, newSize, interpolationQuality,[defaultRep orientation]);


-(UIImage *) oriImageThumbnailAtIndex:(ALAsset *)asset
{
int times=0;
ALAssetRepresentation* assetRepresentation=asset.defaultRepresentation;
long long imageDataSize = [assetRepresentation size];    
uint8_t* imageDataBytes = (uint8_t*) malloc(imageDataSize);
[assetRepresentation getBytes:imageDataBytes fromOffset:0 length:imageDataSize error:nil];
NSData *data = [NSData dataWithBytesNoCopy:imageDataBytes length:imageDataSize freeWhenDone:YES];
UIImage *image2=nil;  
CGImageSourceRef src = CGImageSourceCreateWithData((__bridge CFDataRef) data, NULL);        
CGImageRef thumbnail;
if (src)
{
    float _maxSize=RESIZE_IMAGE_BIG_SIDE;
    thumbnail = CGImageSourceCreateImageAtIndex(src, 0, NULL); // Create scaled image
    image2 = [UIImage imageWithCGImage:thumbnail];        
    CGImageRelease(thumbnail);        
}else{
    NSLog(@"resizeImage:(ALAsset *)asset error!!!");        
}

return image2;
}

UIImage* makeResizedImageRotate(UIImage *inimage,CGSize newSize,CGInterpolationQuality interpolationQuality,UIImageOrientation orient) {
CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
CGImageRef imageRef = inimage.CGImage;

// Compute the bytes per row of the new image
size_t bytesPerRow = CGImageGetBitsPerPixel(imageRef) / CGImageGetBitsPerComponent(imageRef) * newRect.size.width;
bytesPerRow = (bytesPerRow + 15) & ~15;  // Make it 16-byte aligned
  CGBitmapInfo cgbmpinfo=CGImageGetBitmapInfo(imageRef);
CGContextRef bitmap = CGBitmapContextCreate(NULL,
                                            newRect.size.width,
                                            newRect.size.height,
                                            CGImageGetBitsPerComponent(imageRef),
                                            bytesPerRow,
                                            CGImageGetColorSpace(imageRef),
                                            cgbmpinfo);
if(bitmap!=nil)
    CGContextSetInterpolationQuality(bitmap, interpolationQuality);
else
    NSLog(@"bitmap==nil");


// Draw into the context; this scales the image
CGContextDrawImage(bitmap, newRect, imageRef);

// Get the resized image from the context and a UIImage
CGImageRef resizedImageRef = CGBitmapContextCreateImage(bitmap);
UIImage *resizedImage = [UIImage imageWithCGImage:resizedImageRef];
CGContextRelease(bitmap);
CGImageRelease(resizedImageRef);
resizedImage=rotateImage( resizedImage, orient);
// Clean up

// [resizedImage autorelease]; will introduce BAD_ADDRESS
return resizedImage;

}

2 个答案:

答案 0 :(得分:1)

当我使用第三方JPEG解码库(libjpeg-turbo)解码并在2048x2048 UIImage下调整它时,我发现它减少了很多。(甚至总内存也没有减少太多。)所以内存的流动经常发生是由于创建大于2Kx2K的UIImage。希望这会有所帮助。

答案 1 :(得分:0)

我正在使用以下代码

-(UIImage*) generateThumbnailFromImage:(UIImage*)mainImage
{
    UIImage * thumbnail;

    CGSize destinationSize = CGSizeMake(thumbnail image width,thumbnail image height);

    UIGraphicsBeginImageContext(destinationSize);
    [mainImage drawInRect:CGRectMake(0,0,destinationSize.width, destinationSize.height)];
    thumbnail = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return thumbnail;
}

我从来没有遇到过内存警告这个尝试它如何与您的代码一起使用。