使用时压缩jpeg数据时出错

时间:2011-01-16 14:01:02

标签: iphone-sdk-3.0 uiimage cgimage

尝试使用UIImageJPEGRepresentation(image, 0.5f);

压缩某些jpeg数据时出现以下错误
<Error>: Unsupported pixel description - 4 components, 8 bits-per-component, 32 bits-per-pixel
<Error>: CGBitmapContextCreateWithDictionary: failed to create delegate.
<Error>: CGImageDestinationAddImage image could not be converted to destination format.
<Error>: CGImageDestinationFinalize image destination does not have enough images

以下是我下载并压缩数据的代码段...

ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:object.largeimage]];
[request startSynchronous];

NSData *imageData = [request responseData];

UIImage *image = [[UIImage alloc] initWithData:imageData];
NSData *compressedImageData = UIImageJPEGRepresentation(image, 0.5f);

//If the data was compressed properly...
if(compressedImageData) [compressedImageData writeToFile:filePath atomically:YES];

//If it wasn't...
else [imageData writeToFile:filePath atomically:YES];

[image release];

if(request.responseStatusCode == 200)
object.fullImageFilename = filePath;

[request release];

[请求发布];

提前感谢您的任何帮助:)

1 个答案:

答案 0 :(得分:3)

仍然不确定“正确的答案”,但我设法为任何感兴趣的人找到解决方法:)

ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:object.largeimage]];
[request startSynchronous];

NSData *imageData = [request responseData];

UIImage *image = [[UIImage alloc] initWithData:imageData];
NSData *compressedImageData = UIImageJPEGRepresentation(image, 0.5f);

//If the data was compressed properly...
if(compressedImageData) [compressedImageData writeToFile:filePath atomically:NO];

//If it wasn't...
else
{
    UIGraphicsBeginImageContext(image.size);
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    compressedImageData = UIImageJPEGRepresentation(newImage, 0.5f);
    [compressedImageData writeToFile:filePath atomically:NO];
    [newImage release];
}

[image release];

if(request.responseStatusCode == 200)
    object.fullImageFilename = filePath;

[request release];