Here i am converting my image to binary data by category on UIImage which have static method.My Problem is UIImageJPEGRepresentation and UIImagePNGRepresentation are very slow upto 6 second. I need 1 sec solution.Can Anybody help me. Here i pass my image to category method till its size reduce to less than or equal to 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 ; must be equal to something
I reduce your code you were using two times more UIImageJPEGRepresentation try this
- (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;
}