簡體   English   中英

將UIImageView的圖像按比例縮放為填充模式

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM