如何裁剪UIImage
的矩形(屏幕截图中的红色方块)旋转以及使用UIScrollView进行缩放。
UIImageView
的边缘由于旋转而隐藏(UIImageView
转换)。请帮忙。
答案 0 :(得分:0)
嗯,你可以做所有复杂的核心图形事物或做一个简单的UIView
截图。我投票支持简单的解决方案:您需要做的是创建一个新视图,其框架与小矩形所在的框架相同。然后将整个图像视图添加到转换其帧的小视图中,使其看起来相同。然后截取小视图的屏幕截图。完成后,只需将图像视图恢复原样,然后移除小视图。
由于这仍然比较容易,所以在这里完成了一些代码来咀嚼(我没有测试过,所以请在成功后纠正错误)。
- (UIImage *)getScreenshotInRect:(CGRect)frame {
UIImageView *theImageView; //your original image view
UIView *backupSuperView = theImageView.superview; //backup original superview
CGRect backupFrame = theImageView.frame; //backup original frame
UIView *frameView = [[UIView alloc] initWithFrame:frame]; //create new view where the image should be taken at
frameView.clipsToBounds = YES; //not really necessery but can be usefull for cases like using corner radius
[self addSubview:frameView];
theImageView.frame = [theImageView.superview convertRect:theImageView.frame toView:frameView]; //set the new frame for the image view
[frameView addSubview:theImageView];
UIImage *toReturn = [self imageFromView:frameView]; //get the screenshot
theImageView.frame = backupFrame; //reset the image view frame
[backupSuperView addSubview:theImageView]; //reset the image view's superview
[frameView removeFromSuperview];
frameView = nil;
return toReturn;
}
- (UIImage *)imageFromView:(UIView *)view {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, .0f);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
我希望这不会因为你有轮换而中断。如果是这种情况,我建议您创建旋转图像视图所在的另一个视图,并将此视图添加到小视图中。