UIImage的缩小
有时候,项目中,要用到上传图片,从图片库里取出的图片有的太大了,而要上传的时候,会很费时间,而且也没必要太大,所以就把图片综缩小一下,再传!
#pragma UIImagePickerDelagate -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
[self.imagePopoverController dismissPopoverAnimated:YES];
/*
选取成功后在界面上进行显示
*/
//压缩图片
int iWidth = image.size.width;
int iHeight = image.size.height;
if (iWidth>300) {//300你自定义大小,想要弄多大,就弄多大
iWidth = 300;
iHeight = image.size.height*iWidth/image.size.width;
if (iHeight>300) {
iHeight = 300;
iWidth = image.size.width*iHeight/image.size.height;
}
}
image = [self scaleToSize:image :CGSizeMake(iWidth, iHeight)];//主要在这里 NSData* imageData = UIImagePNGRepresentation(image); [self saveImage:imageData WithName:@"pic.jpg"];//保存图片 }
//图片缩放
- (UIImage *)scaleToSize:(UIImage *)image :(CGSize)newsize {
// 创建一个bitmap的context
// 并把它设置成为当前正在使用的context
UIGraphicsBeginImageContext(newsize); // 绘制改变大小的图片
[image drawInRect:CGRectMake(0, 0, newsize.width, newsize.height)]; // 从当前context中创建一个改变大小后的图片
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); // 使当前的context出堆栈
UIGraphicsEndImageContext(); // 返回新的改变大小后的图片
return scaledImage;
}