IOS 截屏


轉: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];

}



























發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章