使用压缩 jpeg 数据时出错

发布于 2024-10-12 20:46:08 字数 1167 浏览 8 评论 0原文

尝试使用 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];

[请求发布];

预先感谢您的帮助:)

I get the following error when trying to compress some jpeg data using UIImageJPEGRepresentation(image, 0.5f);

<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

Here is the code snippet where I download and compress the data...

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];

[request release];

Thanks in advance for any help :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷夜 2024-10-19 20:46:08

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

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];

Still not sure of the "proper answer", but I managed to find a workaround for anyone who's interested :)

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文