gpt4 book ai didi

ios - UIImageJPEGRepresentation 和 UIImagePNGRepresentation 都很慢

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:48:43 27 4
gpt4 key购买 nike

在这里,我在具有静态方法的 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 *数据; 必须等于某物

最佳答案

我减少了你使用两倍以上 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;
}

关于ios - UIImageJPEGRepresentation 和 UIImagePNGRepresentation 都很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15521321/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com