[英]How to crop a UIImageView to a new UIImage in 'aspect fill' mode?
[英]Crop UIImageView's image in scale to fill mode
我有一個UIImageView,它處於按比例縮放以填充模式。 現在我想在imageview中裁剪圖像。 我在此imageview上有一個矩形,用戶可以移動和更改其大小。 選擇特定的尺寸后,我要使用此幀裁剪圖像。 但是由於我的uiimageview模式是可縮放的,因此會弄亂裁剪矩形,並且會裁剪不同的部分。 有沒有人遇到過這個問題。 如何正確種植?
您可以選擇計算或使用以下解決方法:
-(UIImage *)imageFromImageView:(UIImageView *)view withCropRect:(CGRect)cropRect
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * largeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect);
UIImage* img = UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return img;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.