【问题标题】:Crop UIImageView's image in scale to fill mode按比例裁剪 UIImageView 的图像以填充模式
【发布时间】:2012-11-27 04:29:50
【问题描述】:

我有一个 UIImageView,它处于按比例填充模式。现在我想在 imageview 中裁剪图像。我在这个图像视图上有一个矩形,用户可以移动和更改大小。选择特定尺寸后,我想使用此框架裁剪图像。但是由于我的 uiimageview 模式是缩放以填充这会使裁剪矩形变得混乱,并且裁剪了不同的部分。有没有人遇到过这个问题。如何正确裁剪?

【问题讨论】:

  • 转换您的裁剪代码以适应尺寸差异..
  • 你应该阅读坐标空间。听起来您想在一个空间(显示空间)向用户显示图像,但在不同的空间(图像空间)进行裁剪。
  • 看看这个链接可能对你有帮助stackoverflow.com/questions/7253270/…

标签: objective-c ios cocoa-touch uiimageview crop


【解决方案1】:

您的选择是计算或使用此解决方法:

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

【讨论】:

  • 这会导致图像移动到不同的位置。您将如何将原点保留在原始位置?
猜你喜欢
  • 2016-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-28
  • 2021-04-11
相关资源
最近更新 更多