Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIImageJPEG表示获取错误的大小_Ios_Objective C - Fatal编程技术网

Ios UIImageJPEG表示获取错误的大小

Ios UIImageJPEG表示获取错误的大小,ios,objective-c,Ios,Objective C,我想压缩图像。但是,我买错了尺寸。 我确信UIImageJPEG表示法是错误的。如何修复 对不起,我的英语很差 + (NSData *)compressDataWithImg:(UIImage *)originalImage compression:(CGFloat)compression size:(CGFloat)size { NSData *data = [NSData dataWithData:UIImageJPEGRepresentation(originalImage, c

我想压缩图像。但是,我买错了尺寸。 我确信UIImageJPEG表示法是错误的。如何修复 对不起,我的英语很差

 + (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] ;
}

感谢您帮助我

当我使用
uiimagejpegresentation
imageWithData
一次又一次处理图像时,我发现了这一点。图像数据的
长度逐渐增加

测试代码:

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);
日志:

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

只是个测试

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


我发现当我使用
uiimagejpegresentation
imageWithData
一次又一次地处理图像时。图像数据的
长度逐渐增加

测试代码:

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);
日志:

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

只是个测试

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