Ios UIImageJPEGRepresentation和UIImagePNGRepresentation都很慢
在这里,我将我的图像按类别转换为二进制数据,UIImage采用静态方法。我的问题是UIImageJPEGRepresentation和UIImagePNGRepresentation非常慢,最长可达6秒。我需要1秒的解决方案,有人能帮我吗。 在这里,我将图像传递给category方法,直到其大小减小到小于或等于10KBIos UIImageJPEGRepresentation和UIImagePNGRepresentation都很慢,ios,nsdata,image-resizing,uiimagejpegrepresentation,uiimagepngrepresentation,Ios,Nsdata,Image Resizing,Uiimagejpegrepresentation,Uiimagepngrepresentation,在这里,我将我的图像按类别转换为二进制数据,UIImage采用静态方法。我的问题是UIImageJPEGRepresentation和UIImagePNGRepresentation非常慢,最长可达6秒。我需要1秒的解决方案,有人能帮我吗。 在这里,我将图像传递给category方法,直到其大小减小到小于或等于10KB -(NSData *)imageConvertToBinary :(UIImage *) image{ NSLog(@"Image Convert ");
-(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*数据;
必须等于某个值我减少了您的代码,您使用的UIImageJPEG表示比以前多了两倍
- (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;
}
数据尚未初始化