繁体   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