-(UIImage *)saveNewImage:(UIImage )image{
CGFloat maxFileSize = 321024;
CGFloat compression = 0.9f;
UIGraphicsBeginImageContext(CGSizeMake(320, 480));
[image drawInRect:CGRectMake(0,0,320, 480)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *compressedData = UIImageJPEGRepresentation(newImage, compression);
while ([compressedData length] > maxFileSize ) {
if (compression<=0.1) {
compression = 0.9;
}
else
{
compression -= 0.1;
}
UIImage *image_m = [UIImage imageWithData: compressedData];
compressedData = UIImageJPEGRepresentation(image_m, compression);
}
UIImage *compressedImage = [UIImage imageWithData:compressedData];
return compressedImage;
}