iphone开发-图片处理-截屏

来源:互联网 发布:美国制造业数据哪里查 编辑:程序博客网 时间:2024/05/22 06:55

介绍两种图片的处理方法

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();

smallImage就是要截图的大图中的一部分。