捕捉UIScrollView的可见部分后图像的透明部分
问题描述:
我的UIScrollView上有UIImageView。您可以缩放它并裁剪可见部分。捕捉UIScrollView的可见部分后图像的透明部分
这里是我的卡代码:
UIImage *visibleScrollImage = nil;
UIGraphicsBeginImageContextWithOptions(self.scrollView.bounds.size, YES, [UIScreen mainScreen].scale);
{
CGPoint offset = self.scrollView.contentOffset;
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), -offset.x, -offset.y);
[self.scrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
visibleScrollImage = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();
eivc.editImage = visibleScrollImage;
当图像不走的UIScrollView的所有可见的部分,我不知道如何捕捉图像,而UIImageView的透明部分。我试图用上下文和偏移坐标的不同尺寸来猜测,但它没有奏效,因为我显然不理解有关图像,scrollview或/和CGContext的内容。
这里有一些例子,我设置backgroundColor红色的可见性。
编辑:
现在我做了一半的解决方案 我设置imageView.frame这样
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
CGFloat initialImageHeight = result.size.height;
CGFloat initialImageWidth = result.size.width;
CGFloat height = self.scrollView.bounds.size.height;
CGFloat width = self.scrollView.bounds.size.width;
CGFloat frameHeight = height;
CGFloat frameWidth = width;
if ((initialImageHeight/initialImageWidth) > (height/width)) {
frameHeight = width * initialImageHeight/initialImageWidth;
} else if ((initialImageHeight/initialImageWidth) < (height/width)) {
frameWidth = height * initialImageWidth/initialImageHeight;
}
self.imageView.frame = CGRectMake(0, 0, frameWidth, frameHeight);
self.imageView.image = result;
和我做了minimumZoomScale = 1.0
它阻止图像采取不完整的滚动视图可见部分。
答
由于你的描述,我想你的需求,如enter image description here
第一图像是正常的,第二图像缩放图像。让你感到困惑的是,在没有imageView透明部分的情况下捕捉图像,所以当你放大imageView时,imageView的原点和边界会随之变化。你可以获取屏幕图像,然后基于imageView的坐标,计算出一个矩形,这是一个imageView,一个简短的图像,这个图像是你的imageView的图像,代码如下。
- (UIImage*)cropImage{
UIImage *screenImage = [UIImage imageFromView:self];
CGFloat imageViewWidth = self.imageView.frame.size.width;
CGFloat imageViewHeight = self.imageView.frame.size.height;
CGRect rect;
UIImage *clipImage;
if (type == 1) {
if (imageViewHeight < SCREENWIDTH) {
rect = CGRectMake(0, SCREENHEIGHT/2-imageViewHeight/2, SCREENWIDTH, imageViewHeight);
}else{
rect = CGRectMake(0, SCREENHEIGHT/2-SCREENWIDTH/2, SCREENWIDTH, SCREENWIDTH);
}
}else if (type == 2){
if (imageViewWidth < SCREENWIDTH) {
rect = CGRectMake(SCREENWIDTH/2-imageViewWidth/2, SCREENHEIGHT/2-SCREENWIDTH/2, imageViewWidth, SCREENWIDTH);
}else{
rect = CGRectMake(0, SCREENHEIGHT/2-SCREENWIDTH/2, SCREENWIDTH, SCREENWIDTH);
}
}else{
rect = CGRectMake(0, SCREENHEIGHT/2-SCREENWIDTH/2, SCREENWIDTH, SCREENWIDTH);
}
clipImage = [screenImage cropImage:screenImage inRect:rect];
return clipImage;
}
+ (UIImage *) imageFromView:(UIView *)imageView {
CGFloat scale = 1;
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] == YES) {
scale = [[UIScreen mainScreen] scale];
}
if (scale > 1) {
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, scale);
} else {
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 2);
}
CGContextRef context = UIGraphicsGetCurrentContext();
[imageView.layer renderInContext: context];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
}
- (UIImage *)cropImage:(UIImage*)image inRect:(CGRect)rect
{
double (^rad)(double) = ^(double deg) {
return deg/180.0 * M_PI;
};
CGAffineTransform rectTransform;
switch (image.imageOrientation) {
case UIImageOrientationLeft:
rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(90)), 0, -image.size.height);
break;
case UIImageOrientationRight:
rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(-90)), -image.size.width, 0);
break;
case UIImageOrientationDown:
rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(-180)), -image.size.width, -image.size.height);
break;
default:
rectTransform = CGAffineTransformIdentity;
};
rectTransform = CGAffineTransformScale(rectTransform, image.scale, image.scale);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectApplyAffineTransform(rect, rectTransform));
UIImage *result = [UIImage imageWithCGImage:imageRef scale:image.scale orientation:image.imageOrientation];
CGImageRelease(imageRef);
return result;
}
我得到了imageview的范围缩放时改变部分。只是冷静地想出如何调整权利。 所以我试过你的解决方案,这里的交易它不会削减正确的矩形,它不考虑滚动视图偏移量,这里是例子 这里是2个不同的可见部分 [1](http:// imgur的.com/tJw6m8W) [2](http://imgur.com/BCFPHGS) 在这两种情况下我得到这个 [结果](http://imgur.com/6hAW4CW) ,这里是相同的 [ 1](http://imgur.com/BAV2t1y) [2](http://imgur.com/gta91ZR) 结果 [结果](http://imgur.com/BQToolS) 另外我不'了解“类型”变量的用途。 –