3

我从大约 8MP 的原始分辨率图像调整图像大小,我的输出分辨率约为 2MP。有时,它会显示 iPhone4S 设备的内存警告,我在 memorywarning 中做了一些释放,但它有时会使我的程序因 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;

}

4

2 回答 2

1

我发现当我使用 3rd 方 JPEG 解码库(libjpeg-turbo)在 2048x2048 UIImage 下对其进行解码和调整大小时,它减少了很多。(即使总内存也没有减少太多。)所以内存 wanring 经常发生可能是由创建 UIImage 引起的大于 2Kx2K。希望这可以帮助。

于 2012-12-16T02:16:51.160 回答
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;
}

我从来没有遇到过内存警告,尝试一下它如何与你的代码一起工作。

于 2012-11-10T12:53:35.993 回答