ios截圖功能

weixin_33670713發表於2016-05-27

/**

*截圖功能

*/

-(void)screenShot{

UIGraphicsBeginImageContextWithOptions(CGSizeMake(640,960),YES,0);

//設定截圖大小

[[self.viewlayer]renderInContext:UIGraphicsGetCurrentContext()];

UIImage*viewImage =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

CGImageRefimageRef = viewImage.CGImage;

CGRectrect =CGRectMake(0,0,641,SCREHEIGHT+300);//這裡可以設定想要截圖的區域

CGImageRefimageRefRect =CGImageCreateWithImageInRect(imageRef, rect);

UIImage*sendImage = [[UIImagealloc]initWithCGImage:imageRefRect];

//以下為圖片儲存程式碼

UIImageWriteToSavedPhotosAlbum(sendImage,nil,nil,nil);//儲存圖片到照片庫

NSData*imageViewData =UIImagePNGRepresentation(sendImage);

NSArray*paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

NSString*documentsDirectory = [pathsobjectAtIndex:0];

NSString*pictureName=@"screenShow.png";

NSString*savedImagePath = [documentsDirectorystringByAppendingPathComponent:pictureName];

[imageViewDatawriteToFile:savedImagePathatomically:YES];//儲存照片到沙盒目錄

CGImageRelease(imageRefRect);

//從手機本地載入圖片

UIImage*bgImage2 = [[UIImagealloc]initWithContentsOfFile:savedImagePath];

}


關注微信公眾號:

1860174-b8c6c7d8bf6d2eee.jpg

相關文章