UIImageJPEGRepresentation和UIImagePNGRepresentation都很慢。

3

我正在通过UIImage的分类方法将我的图像转换为二进制数据。我的问题是UIImageJPEGRepresentation和UIImagePNGRepresentation非常慢,需要长达6秒。我需要1秒钟的解决方案。有人能帮我吗? 在这里,我将我的图像传递给分类方法,直到其大小减少到小于或等于10kbs。

  -(NSData *)imageConvertToBinary :(UIImage *) image{

        NSLog(@"Image Convert  ");

        //UIImagePNGRepresentation(image);
        NSData *imageData = UIImageJPEGRepresentation(image, .000032);
         NSLog(@"Image Done  ");

        //Change size of image to 10kbs

        int size = imageData.length;
        NSLog(@"SIZE OF IMAGE:First %i ", size);
        NSData *data = UIImageJPEGRepresentation(image, .0032);
        NSLog(@"Start while  ");
        int temp=0;
        while (data.length / 1000 >= 10) {
            image = [UIImage imageWithImage:image andWidth:image.size.width/2 andHeight:image.size.height/2];

            data = UIImageJPEGRepresentation(image, .0032);
            temp++;
            NSLog(@"temp  %u",temp);

        }

        size = data.length;
        NSLog(@"SIZE OF IMAGE:after %i ", size);


        return data;

    }

and also i have category class on UIImage
@implementation UIImage (ImageProcessing)

+(UIImage*)imageWithImage:(UIImage*)image andWidth:(CGFloat)width andHeight:(CGFloat)height
{
    UIGraphicsBeginImageContext( CGSizeMake(width, height));
    [image drawInRect:CGRectMake(0,0,width,height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}
@end

NSData *data; 必须等于某个值

1个回答

4

我简化了你的代码,你曾使用了两次更多的UIImageJPEGRepresentation,请尝试使用以下代码:

- (NSData *)imageConvertToBinary :(UIImage *) image{
    NSData *data ;
    NSLog(@"Start while  ");
    int temp=0;
    while (data.length / 1000 >= 10) {
        image = [UIImage imageWithImage:image andWidth:image.size.width/2 andHeight:image.size.height/2];
        data = UIImageJPEGRepresentation(image, .0032);
        temp++;
        NSLog(@"temp  %u",temp);
    }
    NSLog(@"End while  ");
    int size = data.length;
    NSLog(@"SIZE OF IMAGE:after %i ", size);
    return data;
}

数据尚未初始化。 - DawnSong

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接