首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIImageJPEGRepresentation和UIImagePNGRepresentation都很慢

UIImageJPEGRepresentation和UIImagePNGRepresentation都很慢
EN

Stack Overflow用户
提问于 2013-03-20 10:45:25
回答 1查看 3.6K关注 0票数 3

在这里,我把我的图像按类别转换为UIImage上的二进制数据,其中存在静态method.My问题,UIImageJPEGRepresentation和UIImagePNGRepresentation速度非常慢,达到6秒。我需要1秒solution.Can,有人来帮我。在这里,我把我的图像传递给类别方法,直到它的大小缩小到小于或等于10公斤。

代码语言:javascript
复制
  -(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;必须等于

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-20 11:17:31

我减少了您使用的代码,您使用的UIImageJPEGRepresentation次数是以前的两倍--试一试

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

https://stackoverflow.com/questions/15521321

复制
相关文章

相似问题

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