问题
I am trying to save an UIImage
object to an .jpeg file in device
and I am using this code:
-(void)saveImageToDocumentsDirectory:(UIImage *)mimage withFileName:(NSString *)fileName
{
UIImageWriteToSavedPhotosAlbum(mimage,nil,nil,nil);
NSData *dataForJPEGFile = UIImageJPEGRepresentation(mimage, 1.0);
NSError *error2 = nil;
if (![dataForJPEGFile writeToFile:[self getDirectoryFilePath:fileName] options:NSAtomicWrite error:&error2])
{
return;
}
}
It will save the image of .jpeg type but take too much memory as compare to the UIImageWriteToSavedPhotosAlbum(mimage,nil,nil,nil)
method, which saves the same image object in .jpeg type and same quality.
My question is why so..??
回答1:
You are setting the quality to be really high (1.0), I guess 0.6 or 0.7 would be more than enough for jpeg files as I read somewhere.
NSData *dataForJPEGFile = UIImageJPEGRepresentation(mimage, 0.65);
Maybe you can find the appropriate quality depending on your app use, for me sometimes 0.5 is more than enough.
来源:https://stackoverflow.com/questions/34740631/why-jpeg-file-save-via-writetofile-using-uiimagejpegrepresentation-method-have