cdu*_*uck 12 objective-c uiimagepickercontroller uiimage ios
我有一个应用程序,让用户用他/她的iPhone拍照,并将其用作应用程序的背景图像.我用UIImagePickerController
它让用户拍照并将背景UIImageView
图像设置为返回的UIImage
对象.
IBOutlet UIImageView *backgroundView;
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
backgroundView.image = image;
[self dismissModalViewControllerAnimated:YES];
}
Run Code Online (Sandbox Code Playgroud)
一切正常.如何减小UIImage
480x320 的大小,以便我的应用程序可以节省内存?我不在乎我是否放弃任何图像质量.
提前致谢.
Ben*_*ieb 16
您可以创建图形上下文,将图像绘制为所需比例的图像,然后使用返回的图像.例如:
UIGraphicsBeginImageContext(CGSizeMake(480,320));
CGContextRef context = UIGraphicsGetCurrentContext();
[image drawInRect: CGRectMake(0, 0, 480, 320)];
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)
Kai*_*Kai 15
我知道这个问题已经解决了,但是如果有人(像我一样)想要保持纵横比来缩放图像,这段代码可能会有所帮助:
-(UIImage *)resizeImage:(UIImage *)image toSize:(CGSize)size
{
float width = size.width;
float height = size.height;
UIGraphicsBeginImageContext(size);
CGRect rect = CGRectMake(0, 0, width, height);
float widthRatio = image.size.width / width;
float heightRatio = image.size.height / height;
float divisor = widthRatio > heightRatio ? widthRatio : heightRatio;
width = image.size.width / divisor;
height = image.size.height / divisor;
rect.size.width = width;
rect.size.height = height;
if(height < width)
rect.origin.y = height / 3;
[image drawInRect: rect];
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return smallImage;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
19732 次 |
最近记录: |