介绍两种图片的处理方法
1,截获当前的屏幕图
2,从大图中截图一小块图
【1】当前的窗口是一个view
CGSize mSize = self.frame.size;
//这个size定义图片的大小
UIGraphicsBeginImageContext(mSize);
//读取当前画布的内容
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *tempImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
#if 0
NSString *path = [NSHomeDirectory() stringByAppendingString:@"/tempImage.jpg"];
NSLog(@"====%@",path);
if ([UIImageJPEGRepresentation(tempImage, 1) writeToFile:path atomically:YES]) {
NSLog(@"success");
}
else {
NSLog(@"failed");
}
#endif
【2】从大图中截取一小图
//大图bigImage
//定义myImageRect,截图的区域
CGRect myImageRect = CGRectMake(10.0, 10.0, 57.0, 57.0);
UIImage* bigImage= [UIImage imageNamed:@"picture.png"];
CGImageRef imageRef = bigImage.CGImage;
CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);
CGSize size;
size.width = 57.0;
size.height = 57.0;
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, myImageRect, subImageRef);
UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
UIGraphicsEndImageContext();