ios截屏功能

/**

*截图功能

*/

-(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];

}


关注微信公众号:

ios截屏功能_第1张图片

你可能感兴趣的:(ios截屏功能)