对于jpg图片来说,有损压缩因子设置为0.5 可以大大减少图片的体积,而对图片的质量几乎没有太大影响:
下面是测试图片结果:
// UIImage *image_jpg = [UIImage imageNamed:@"jpgtest.jpg"];
// UIImage *image_png = [UIImage imageNamed:@"pngtest-1.png"];
//
// NSData *data_pngfrompng = UIImagePNGRepresentation(image_png);
// NSData *data_pngfromjpg = UIImagePNGRepresentation(image_jpg);
// NSData *data_jpgfrompng = UIImageJPEGRepresentation(image_png, 0.5);
// NSData *data_jpgfromjpg = UIImageJPEGRepresentation(image_jpg, 0.5);
//
// NSString *documents = [NSHomeDirectory() stringByAppendingString:@"/Documents/"];
//
// NSString *pathpngfrompng = [documents stringByAppendingString:@"data_pngfrompng.png"];
// NSString *pathpngfromjpg = [documents stringByAppendingString:@"data_pngfromjpg.png"];
// NSString *pathjpgfrompng = [documents stringByAppendingString:@"data_jpgfrompng.jpg"];
// NSString *pathjpgfromjpg = [documents stringByAppendingString:@"data_jpgfromjpg.jpg"];
//
// [data_pngfrompng writeToFile:pathpngfrompng atomically:YES];
// [data_pngfromjpg writeToFile:pathpngfromjpg atomically:YES];
// [data_jpgfrompng writeToFile:pathjpgfrompng atomically:YES];
// [data_jpgfromjpg writeToFile:pathjpgfromjpg atomically:YES];
UIImage *image_jpg = [UIImage
imageNamed:@"2231249403.jpg"];
NSData *data_jpgfromjpg =
UIImageJPEGRepresentation(image_jpg, 0.5);
NSString *documents = [NSHomeDirectory()
stringByAppendingString:@"/Documents/"];
NSString *pathjpgfromjpg = [documents
stringByAppendingString:@"datcccc.jpg"];
[data_jpgfromjpg
writeToFile:pathjpgfromjpg
atomically:YES];
下面是测试图片结果:
// UIImage *image_jpg = [UIImage imageNamed:@"jpgtest.jpg"];
// UIImage *image_png = [UIImage imageNamed:@"pngtest-1.png"];
//
// NSData *data_pngfrompng = UIImagePNGRepresentation(image_png);
// NSData *data_pngfromjpg = UIImagePNGRepresentation(image_jpg);
// NSData *data_jpgfrompng = UIImageJPEGRepresentation(image_png, 0.5);
// NSData *data_jpgfromjpg = UIImageJPEGRepresentation(image_jpg, 0.5);
//
// NSString *documents = [NSHomeDirectory() stringByAppendingString:@"/Documents/"];
//
// NSString *pathpngfrompng = [documents stringByAppendingString:@"data_pngfrompng.png"];
// NSString *pathpngfromjpg = [documents stringByAppendingString:@"data_pngfromjpg.png"];
// NSString *pathjpgfrompng = [documents stringByAppendingString:@"data_jpgfrompng.jpg"];
// NSString *pathjpgfromjpg = [documents stringByAppendingString:@"data_jpgfromjpg.jpg"];
//
// [data_pngfrompng writeToFile:pathpngfrompng atomically:YES];
// [data_pngfromjpg writeToFile:pathpngfromjpg atomically:YES];
// [data_jpgfrompng writeToFile:pathjpgfrompng atomically:YES];
// [data_jpgfromjpg writeToFile:pathjpgfromjpg atomically:YES];
UIImage *image_jpg = [UIImage
imageNamed:@"2231249403.jpg"];
NSData *data_jpgfromjpg =
UIImageJPEGRepresentation(image_jpg, 0.5);
NSString *documents = [NSHomeDirectory()
stringByAppendingString:@"/Documents/"];
NSString *pathjpgfromjpg = [documents
stringByAppendingString:@"datcccc.jpg"];
[data_jpgfromjpg
writeToFile:pathjpgfromjpg
atomically:YES];
NSLog(@"documents=%@",documents);