首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIImageJPEGRepresentation获取错误的大小

UIImageJPEGRepresentation获取错误的大小
EN

Stack Overflow用户
提问于 2017-08-04 15:39:17
回答 1查看 655关注 0票数 1

我想压缩UIImage。然而,我得到了一个错误的尺寸。我肯定UIImageJPEGRepresentation会为我糟糕的英语而道歉的wrong.how

代码语言:javascript
复制
 + (NSData *)compressDataWithImg:(UIImage *)originalImage compression:(CGFloat)compression size:(CGFloat)size {
    NSData *data = [NSData dataWithData:UIImageJPEGRepresentation(originalImage, compression)];
    if ((data.length / 1024.0) > size)
    {
        compression = compression - 0.1;
        if (compression > 0) {
            return [[self class] compressDataWithImg:originalImage compression:compression size:(CGFloat)size];
        }else{
            return data;
        }
    }else{
        return data;
    }
    return data;
}

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *fullImage = info[UIImagePickerControllerOriginalImage];
    fullImage = [fullImage fixOrientationToUp];

    UIImage *imgData = [fullImage scaleToSizeWithLimit:0];
    NSData *data = [Util compressDataWithImg:imgData compression:0.9 size:256]; // this is right:size:2M
    UIImage *image = [UIImage imageWithData:data];//i use this UIImage

    NSData *xxx = [NSData dataWithData: UIImageJPEGRepresentation(image,1) ];// wrong size:7~8M WHY?...

    if (self.imageSelectBlock) {
        self.imageSelectBlock(image);
    }

    [picker dismissViewControllerAnimated:YES completion:NULL] ;
}

谢谢你帮我

EN

回答 1

Stack Overflow用户

发布于 2017-08-04 17:07:02

我发现当我使用UIImageJPEGRepresentationimageWithData时,会一次又一次地处理图像。图像数据的length逐渐增加。

测试代码:

代码语言:javascript
复制
UIImage *image = [UIImage imageNamed:@"test.png"];

NSLog(@"first: %lu", UIImageJPEGRepresentation(image, 1.0).length);
NSLog(@"second: %lu", UIImageJPEGRepresentation([UIImage imageWithData:UIImageJPEGRepresentation(image, 1.0)], 1.0).length);
NSLog(@"third: %lu", UIImageJPEGRepresentation([UIImage imageWithData:UIImageJPEGRepresentation([UIImage imageWithData:UIImageJPEGRepresentation(image, 1.0)], 1.0)], 1.0).length);

日志:

代码语言:javascript
复制
first: 361586
second: 385696
third: 403978

我认为这个问题是由UIImageJPEGRepresentation引起的。

这只是个测试。

我发现了一个相关的问题:

When i am using UIImagePNGRepresentation or UIImageJPEGRepresentation for converting UIImage into NSdata, the image size is too much increased

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45500865

复制
相关文章

相似问题

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