NSData 与UIimage 关系 然后 压缩
1 //NSData转换为UIImage
2 NSData *imageData = [NSData dataWithContentsOfFile: imagePath];
3 UIImage *image = [UIImage imageWithData: imageData];
4
5 //UIImage转换为NSData
6 NSData *imageData = UIImagePNGRepresentation(aimae);
1 //对图片尺寸进行压缩--
2 -(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
3 {
4 // Create a graphics image context
5 UIGraphicsBeginImageContext(newSize);
6
7 // Tell the old image to draw in this new context, with the desired
8 // new size
9 [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
10
11 // Get the new image from the context
12 UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
13
14 // End the context
15 UIGraphicsEndImageContext();
16
17 // Return the new image.
18 return newImage;
19 }
//按比例压缩
NSData *imageData = UIImageJPEGRepresentation(self.uploadImage , 0.75);
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步