UIImage保存到文件,PNG/JPEG自适应
UIImage保存到文件,PNG/JPEG自适应
+ (BOOL)writeImage:(UIImage*)image toFileAtPath:(NSString*)aPath
{
if ((image == nil) || (aPath == nil) || ([aPathisEqualToString:@""]))
return NO;
@try
{
NSData *imageData = nil;
NSString *ext = [aPath pathExtension];
if ([ext isEqualToString:@"png"])
{
imageData = UIImagePNGRepresentation(image);
}
else
{
// the rest, we write to jpeg
// 0. best, 1. lost. about compress.
imageData = UIImageJPEGRepresentation(image,0);
}
if ((imageData == nil) || ([imageData length] <= 0))
return NO;
[imageData writeToFile:aPath atomically:YES];
return YES;
}
@catch (NSException *e)
{
NSLog(@"create thumbnail exception.");
}
return NO;
}
转自:http://www.cocoadev.cn/iPhone-iPad/iPhone-Uimage-Write-to-file.asp