在 UIScrollView 问题中裁剪缩放图像
Posted
技术标签:
【中文标题】在 UIScrollView 问题中裁剪缩放图像【英文标题】:Crop zoomed image in UIScrollView issue 【发布时间】:2014-07-29 12:39:41 【问题描述】:在通过this link 之后,我的代码的问题是输出图像无法设置正确的 x 和 y 值,因为裁剪后的图像在结果图像中似乎有 0 和 0,而与我缩放的位置(或滚动的位置)无关计算偏移量)。这是我尝试过的。
- (IBAction)crop:(id)sender
float zoomScale = 1.0f / [self.scroll zoomScale];
CGRect rect;
NSLog(@"contentOffset is :%f,%f",[self.scroll contentOffset].x,[self.scroll contentOffset].y);
rect.origin.x = self.scroll.contentOffset.x * zoomScale;
rect.origin.y = self.scroll.contentOffset.y * zoomScale;
rect.size.width = self.scroll.bounds.size.width * zoomScale;
rect.size.height = self.scroll.bounds.size.height * zoomScale;
UIGraphicsBeginImageContextWithOptions( CGSizeMake(rect.size.width, rect.size.height),
NO,
0.);
NSLog(@"rect offset is :%f,%f",rect.origin.x,rect.origin.y);
CGPoint point = CGPointMake(-rect.origin.x, -rect.origin.y); **//even though above NSLog have some values, but output image is unable to set proper x and y values as cropped image seems to have 0 and 0 in the resultant image.**
[[self.imagV image] drawAtPoint:point
blendMode:kCGBlendModeCopy
alpha:1];
self.croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
DTImageViewController *imageViewController = [[DTImageViewController alloc] initWithNibName:@"DTImageViewController" bundle:nil];
imageViewController.image = self.croppedImage;
[self.navigationController pushViewController:imageViewController animated:YES];
【问题讨论】:
【参考方案1】:与已发布的类似代码,但仅占用整个 UIScrollView
边界而未传递 CGRect
-(void)takeScreenShotOfScrollView
UIGraphicsBeginImageContextWithOptions(scrollView.bounds.size, YES, [UIScreen mainScreen].scale);
CGPoint offset = scrollView.contentOffset;
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), -offset.x, -offset.y);
[scrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
img = [SDImageHelper imageWithImage:img_image scaledToSize:CGSizeMake(769, 495)];
奖金:
裁剪方法
+(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
//UIGraphicsBeginImageContext(newSize);
// In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
// Pass 1.0 to force exact pixel size.
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
【讨论】:
【参考方案2】:我为此使用了以下代码,它完全符合预期:
- (UIImage *) croppedImageOfView:(UIView *) view withFrame:(CGRect) rect
UIGraphicsBeginImageContextWithOptions(rect.size,NO,0.0);
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), -rect.origin.x, -rect.origin.y);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *visibleScrollViewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return visibleScrollViewImage;
- (void) crop
CGRect neededRect = CGRectMake(50,50,100,100);
UIImage *image = [self croppedImageOfView:_scrollView withFrame:neededRect];
即使内容被缩放也能很好地工作,唯一必须明智地计算的是需要的区域CGRect
。
【讨论】:
以上是关于在 UIScrollView 问题中裁剪缩放图像的主要内容,如果未能解决你的问题,请参考以下文章
以全分辨率从 UIScrollView 中捕获缩放的 UIImageView
将 UIScrollview 中显示的图像裁剪为覆盖层中显示的矩形