这两天开发中需要截取屏幕中指定位置的图片,搜罗了下没发现多少资料,大部分都是截取全屏的功能实现。
幸好发现可以截取图片指定rect区域的实现,示例代码如下:
- (void)loadView {
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation: UIStatusBarAnimationSlide];
UIImage *image=[UIImage imageNamed:@"1.jpg"];
CGRect rect = CGRectMake(60, 80, 331, 353);//创建矩形框
UIImageView *contentView = [[UIImageView alloc] initWithFrame:rect];
contentView.image=[UIImage imageWithCGImage:CGImageCreateWithImageInRect([image CGImage], rect)];
self.view=[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[self.view addSubview:contentView];
[image release];
}
上面代码是在image的基础上面截图,所以决定先截取全屏获取全屏图片,再在全屏截图的基础上进行部分截图。
附上全屏截图示例代码:
UIGraphicsBeginImageContext(self.shakeResView.bounds.size);
[self.shakeResView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();