我有UIScrollView和UIImageView。您可以缩放它并裁剪可见部分。
这是我的snap代码:
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
它阻止图像不是完整的scrollview可见部分。
答案 0 :(得分: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;
}