iOS:从旋转图像视图裁剪图像

iOS:从旋转图像视图裁剪图像,ios,Ios,如何裁剪使用UIScrollView旋转和缩放的UIImage矩形(屏幕截图中的红色正方形) UIImageView的边由于旋转(UIImageView变换)而隐藏。请帮忙 好吧,你可以做所有复杂的核心图形,或者做一个简单的UIView屏幕截图。我投票赞成简单的解决方案:你要做的是创建一个新的视图,其框架与小矩形所在的位置相同。然后将整个图像视图添加到该小视图中,转换其框架,使其看起来相同。然后拍摄小视图的屏幕截图。完成后,只需将图像视图放回原样,然后删除小视图 因为这更容易说,然后做这里有一些

如何裁剪使用UIScrollView旋转和缩放的
UIImage
矩形(屏幕截图中的红色正方形)

UIImageView
的边由于旋转(
UIImageView
变换)而隐藏。请帮忙


好吧,你可以做所有复杂的核心图形,或者做一个简单的
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;
}