UIImage *image01 = [UIImage imageNamed:@"002.png"];//原图 NSData *dataOf02 =UIImageJPEGRepresentation(image01, 0.5);//压缩图片内容,不影响图片的size,得到一个原大小,但更模糊的图片。 UIImage *image02 = [UIImage imageWithData:dataOf02];//压缩图02 UIImage *image03 = [self makeThumbnailFromImage:image01 scale:0.5];//压缩图03 NSLog(@"001===%@",NSStringFromCGSize(image01.size)); NSLog(@"002===%@",NSStringFromCGSize(image02.size)); NSLog(@"003===%@",NSStringFromCGSize(image03.size)); NSData *data001 = UIImageJPEGRepresentation(image01, 1.0); NSData *data002 = UIImageJPEGRepresentation(image02, 1.0); NSData *data003 = UIImageJPEGRepresentation(image03, 1.0); NSLog(@"002.lenght===%d",data001.length); NSLog(@"002.lenght===%d",data002.length); NSLog(@"003.lenght===%d",data003.length); //看一下效果 UIImageView *imageView01 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image01.size.width, image01.size.height)]; imageView01.image = image01; [self.view addSubview:imageView01]; UIImageView *imageView02 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, image02.size.width, image02.size.height)]; imageView02.image = image02; [self.view addSubview:imageView02]; UIImageView *imageView03 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 200, image03.size.width, image03.size.height)]; imageView03.image = image03; [self.view addSubview:imageView03];
//手动实现图片压缩,可以写到分类里,封装成常用方法。按照大小进行比例压缩,改变了图片的size。 - (UIImage *)makeThumbnailFromImage:(UIImage *)srcImage scale:(double)imageScale { UIImage *thumbnail = nil; CGSize imageSize = CGSizeMake(srcImage.size.width * imageScale, srcImage.size.height * imageScale); if (srcImage.size.width != imageSize.width || srcImage.size.height != imageSize.height) { UIGraphicsBeginImageContext(imageSize); CGRect imageRect = CGRectMake(0.0, 0.0, imageSize.width, imageSize.height); [srcImage drawInRect:imageRect]; thumbnail = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } else { thumbnail = srcImage; } return thumbnail; }
利用UIImageJPEGRepresentation与UIGraphicsBeginImageContext进行图片压缩的简单比较,布布扣,bubuko.com
利用UIImageJPEGRepresentation与UIGraphicsBeginImageContext进行图片压缩的简单比较
原文:http://blog.csdn.net/u011439689/article/details/26365029