IOS 截屏

UIGraphicsBeginImageContext (CGSize)截图 ,是从屏幕原点开始截取size大小的图片
如何截取任意起点开始 size 大小的图片,办法就是用CGContextTranslateCTM转换原点坐标//导入头文件//创建一个基于位图的图形上下文并指定大小为CGSizeMake(200,400)


UIGraphicsBeginImageContext(CGSize)截图,是从屏幕原点开始截取size大小的图片
如何截取任意起点开始 size大小的图片,办法就是用CGContextTranslateCTM转换原点坐标//导入头文件 //创建一个基于位图的图形上下文并指定大小为CGSizeMake(200,400)
Java代码
UIGraphicsBeginImageContext(CGSizeMake(200,400));

//renderInContext呈现接受者及其子范围到指定的上下文
[self.view.layerrenderInContext:UIGraphicsGetCurrentContext()];

//返回一个基于当前图形上下文的图片
UIImage *aImage =UIGraphicsGetImageFromCurrentImageContext();

//移除栈顶的基于当前位图的图形上下文
UIGraphicsEndImageContext();

//以png格式返回指定图片的数据
imageData =UIImagePNGRepresentation(aImage);

Java代码
#import<QuartzCore/QuartzCore.h>
//将整个self.view大小的图层形式创建一张图片image UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage*image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//然后将该图片保存到图片图
UIImageWriteToSavedPhotosAlbum(image,self,nil,nil);

点击按钮,截取一短图片
-(IBAction)cutBtnClick:(id)sender;
{


CGRect frame=imageView.frame;
imageView.frame=CGRectMake(-420, 300, frame.size.width, frame.size.height);

// 第一种方法  从原点开始截取
// UIGraphicsBeginImageContext(CGSizeMake(320,100));
// [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
// UIImage*image=UIGraphicsGetImageFromCurrentImageContext();
// UIGraphicsEndImageContext();
//



// 第二种方法 按制定区域截取


UIGraphicsBeginImageContext(CGSizeMake(320, 200));
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage*parentImage=UIGraphicsGetImageFromCurrentImageContext();

CGImageRef imageRef = parentImage.CGImage;
CGRect myImageRect=CGRectMake(0, 100, 320, 100);
CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);

CGSize size=CGSizeMake(320, 100);
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, myImageRect, subImageRef);

UIImage* image = [UIImage imageWithCGImage:subImageRef];
UIGraphicsEndImageContext();


CGImageRelease(imageRef);
UIGraphicsEndImageContext();



imageView.image=image;


[UIView beginAnimations:@"ToggleViews" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

imageView.frame=CGRectMake(0, 300, frame.size.width, frame.size.height);
[UIView commitAnimations];
}

转自:http://blog.csdn.net/ch_soft/article/category/862067
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值