简体   繁体   English

将UIImageView的图像按比例缩放为填充模式

[英]Crop UIImageView's image in scale to fill mode

I have one UIImageView which is in scale to fill mode. 我有一个UIImageView,它处于按比例缩放以填充模式。 Now i want to crop the image in imageview. 现在我想在imageview中裁剪图像。 I have one rectangle on this imageview which user can move and change the size. 我在此imageview上有一个矩形,用户可以移动和更改其大小。 After particular size is selected i want to crop the image using this frame. 选择特定的尺寸后,我要使用此帧裁剪图像。 But since my mode of uiimageview is scale to fill this messes the cropping rectangle and different part is cropped. 但是由于我的uiimageview模式是可缩放的,因此会弄乱裁剪矩形,并且会裁剪不同的部分。 Has any one faced this problem. 有没有人遇到过这个问题。 How to crop properly ? 如何正确种植?

Your options are to calculate, or use this walkaround: 您可以选择计算或使用以下解决方法:

-(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