iOS获取UIImageView的可视图像部分

Wal*_*ain 2 iphone objective-c ios

我想要得到的可见部分UIImageUIImageView.UIImageView占据整个屏幕.添加了捏合和平移手势UIImageView.因此,用户可以平移/缩放图像视图.平移/缩放后,我想仅裁剪图像视图的可见部分.我尝试了很多方法.但它给我带来了错误的图像部分.

提前致谢.

ZGl*_*XNt 10

将QuartzCore框架包含在您的项目中并实现此方法,以便从图像视图中获取可见的图像部分:(一个注意:我的意思是您在项目中使用ARC)

#import <QuartzCore/QuartzCore.h>

- (UIImage*)imageFromImageView:(UIImageView*)imageView
{
    UIGraphicsBeginImageContext(imageView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextRotateCTM(context, 2*M_PI);

    [imageView.layer renderInContext:context];
    UIImage *image =  UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}
Run Code Online (Sandbox Code Playgroud)