IOS 截屏

来源:互联网 发布:淘宝联盟怎么操作步骤 编辑:程序博客网 时间:2024/05/21 05:04


转:http://blog.csdn.net/ch_soft/article/details/7338012



UIGraphicsBeginImageContext (CGSize)截图 ,是从屏幕原点开始截取size大小的图片

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


UIGraphicsBeginImageContextCGSize)截图,是从屏幕原点开始截取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(-420300, frame.size.width, frame.size.height);

   

//    第一种方法  从原点开始截取

//    UIGraphicsBeginImageContext(CGSizeMake(320,100));

//    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];   

//    UIImage*image=UIGraphicsGetImageFromCurrentImageContext();    

//    UIGraphicsEndImageContext(); 

//    

    

    

    

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


    

    UIGraphicsBeginImageContext(CGSizeMake(320200));

    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];   

    UIImage*parentImage=UIGraphicsGetImageFromCurrentImageContext();

    

    CGImageRef imageRef = parentImage.CGImage;

    CGRect myImageRect=CGRectMake(0100320100);

    CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);

    

    CGSize size=CGSizeMake(320100);

    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(0300, frame.size.width, frame.size.height);

    [UIView commitAnimations];

}