简体   繁体   English

裁剪截图并保存图像

[英]Crop taken screenshot and save image

I'm using this code to take a screenshot of my specific UIView which is inside in my main view. 我正在使用此代码来拍摄特定UIView的屏幕快照,该屏幕快照位于主视图中。

UIGraphicsBeginImageContextWithOptions(_myView.frame.size, NO, [UIScreen mainScreen].scale);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
UIImage *imageSave = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Main view size is (0,0,320,480) lets say, and myView size is (10,20,200,150) 可以说,主视图大小为(0,0,320,480),而myView大小为(10,20,200,150)

Now i would like to capture/take a screenshot only of myView. 现在我只想捕获/拍摄myView的屏幕截图。

The above code doesn't capture exactly how i want, any suggestions? 上面的代码没有完全捕获我想要的任何建议?

Try this, it works for me. 试试这个,对我有用。

- (UIImage *)snapshotOfView:(UIView *)view
{
    UIGraphicsBeginImageContext(view.bounds.size);

    [view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return viewImage;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM