首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用压缩jpeg数据时出错

使用压缩jpeg数据时出错
EN

Stack Overflow用户
提问于 2011-01-16 22:01:02
回答 1查看 1.2K关注 0票数 0

当我尝试使用UIImageJPEGRepresentation(image, 0.5f);压缩一些jpeg数据时,我得到了以下错误

代码语言:javascript
复制
<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

下面是我下载和压缩数据的代码片段……

代码语言:javascript
复制
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];

请求释放;

提前感谢您的帮助:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-01-17 02:22:15

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

代码语言:javascript
复制
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];
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4705737

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档