iphone开发-图片处理-截屏

本文介绍了两种iOS平台上的图片处理方法:一是获取当前屏幕的截图;二是从一张大图中截取出一小部分作为新图片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


介绍两种图片的处理方法

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就是要截图的大图中的一部分。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值