场景
我正在开发一款照片分享应用。该应用程序需要允许用户在将图像发送到服务器之前裁剪图像。我正在使用一个 UIScrollView
和一个包含我希望用户裁剪的图像的 UIImageView
subview 。
我尝试了什么
我已经尝试使用 StackOverflow 上的一些解决方案来做到这一点,但它们涉及在用户缩放后截取 UIScrollView
框架的屏幕截图。这不起作用的原因是,当用户在 iPhone 5(320x320,屏幕宽度的完美正方形)上拍摄屏幕截图时,当该照片上传并显示在 iPhone 6 Plus(更大的屏幕尺寸)上时,图像将被像素化。
问题
如何在 UIScrollView
中获取可能放大 3000x3000 的图像并提取尺寸为 UIScrollView
帧 (320x320) 的图像 但是 缩放到 640x640 而不 丢失图像分辨率?
插图
最佳答案
首先,你可以子类化包含UIImageView的UIScrollView,初始化这个UIScrollView和UIImageView,给UIImageView分配一个图像实例,这样它就可以显示了。
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.delegate = self;
self.maximumZoomScale = 4.0f;
self.showsHorizontalScrollIndicator = NO;
self.showsVerticalScrollIndicator = NO;
[self loadImageView:frame];
}
return self;
}
- (void) loadImageView:(CGRect) frame {
if (!_imageView) {
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,(frame.size.height-frame.size.width)/2, frame.size.width, frame.size.width)];
_imageView.contentMode = UIViewContentModeScaleAspectFit;
_imageView.center = self.center;
[self addSubview:_imageView];
}
}
//call this menthod to assginment image to imageView
- (void)showImage:(UIImage*)image{
self.imageView.image = image;
[self handleImageType];
}
其次,可以在viewForZoomingInScrollView和scrollViewDidZoom中处理,代码如下:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return _imageView;//your imageView inside UIScrollView
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
CGSize boundSize = self.bounds.size;//scrollView bounds
CGFloat boundWidth = boundSize.width;
CGFloat boundHeight = boundSize.height;
CGSize imageSize = self.imageView.image.size;
CGFloat imageWidth = imageSize.width;
CGFloat imageHeight = imageSize.height;
CGFloat zoomScale = scrollView.zoomScale;
DLog(@"zoomScale === %f",zoomScale);
[self normalScaleViewDidZoom:boundWidth boundHeight:boundHeight imageWidth:imageWidth imageHeight:imageHeight scrollView:scrollView];
}
- (void)handleImageType{
CGSize imageSize = self.imageView.image.size;//imageView's image size
CGFloat imageWidth = imageSize.width;
CGFloat imageHeight = imageSize.height;
if (imageWidth > imageHeight) {
type = 1;
} else if (imageWidth < imageHeight){
type = 2;
} else if (imageWidth == imageHeight) {
type = 3;
}
}
- (void)normalScaleViewDidZoom:(CGFloat)boundWidth boundHeight:(CGFloat)boundHeight imageWidth:(CGFloat)imageWidth imageHeight:(CGFloat)imageHeight scrollView:(UIScrollView*)scrollView{
CGFloat zoomScale = scrollView.zoomScale;
if (type == 1) {
self.contentSize = CGSizeMake(boundWidth/imageHeight*imageWidth*zoomScale,boundWidth*zoomScale + (boundHeight-boundWidth));
} else if (type == 2){
self.contentSize = CGSizeMake(boundWidth*zoomScale,boundWidth/imageWidth*imageHeight*zoomScale + (boundHeight-boundWidth) );
} else if (type == 3) {
self.contentSize = CGSizeMake(boundWidth*zoomScale,boundHeight-boundWidth+boundWidth*zoomScale);
}
[self zoomCenter:scrollView];
}
- (void)zoomCenter:(UIScrollView*)scrollView{
CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?(scrollView.bounds.size.width - scrollView.contentSize.width)/2 : 0.0;
CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?(scrollView.bounds.size.height - scrollView.contentSize.height)/2 : 0.0;
self.imageView.center = CGPointMake(scrollView.contentSize.width/2 + offsetX,scrollView.contentSize.height/2 + offsetY);
}
以上代码都是我在项目中应用的,希望对你有用:)
关于ios - Objective-C:从 UIScrollView 获取缩放的裁剪图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33289634/