iOS为UIImageView获取图像的可见部分
我想从UIImageView
的UIImage
的可见部分。 UIImageView
占用整个屏幕。 捏和平手势被添加到UIImageView
。 所以,用户可以平移/缩放imageview。 平移/缩放后,我只想裁剪图像视图的可见部分。 我已经尝试了一些方法。 但它回报我错了图像的一部分。
提前致谢。
在你的项目中包含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; }
谢谢你们。 我已经想出了解决scheme。 这是我用来获取图像可见部分的代码。 (请记住,缩放或平移时,图像不完全可见,因为它正在全屏显示)
- (UIImage *)cropVisiblePortionOfImageView:(UIImageView *)imageView { CGFloat zoomScaleX=imageView.frame.size.width/initialWidth; CGFloat zoomScaleY=imageView.frame.size.height/initialHeight; CGSize zoomedSize=CGSizeMake(initialWidth*zoomScaleX,initialHeight*zoomScaleY); UIGraphicsBeginImageContext(zoomedSize); [imageView.image drawInRect:CGRectMake(0, 0, zoomedSize.width, zoomedSize.height)]; UIImage *zoomedImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIGraphicsBeginImageContext(CGSizeMake(initialWidth, initialHeight)); [zoomedImage drawAtPoint:CGPointMake(imageView.frame.origin.x, imageView.frame.origin.y)]; UIImage *cropedImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return cropedImage; }
- “Chrome Apps for Mobile”是否在每个平台上使用本地networking浏览?
- iPhone的UIViewController进入状态栏
- 为什么使用“viewWithTag”和“dequeueReusableCellWithIdentifier”?
- 在iPad上运行Mobile Safari时即将切换的页面隐藏事件不会触发
- iOS Twitter API; 如何检索国家内最新的推文?
- iOS表情符号是一项严肃的业务-第2部分:模仿iOS 8.3的表情符号键盘
- iOS在应用程序内注册推送通知
- AVAudioPlayer在模拟器上工作,但不在真实设备上
- UIAlertView show()行为为UIAlertController