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

你可能感兴趣的:(ios)