ios截屏功能

作者: 71150ce14a00 | 来源:发表于2016-05-27 18:31 被阅读155次

    /**

    *截图功能

    */

    -(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截屏功能

        本文链接:https://www.haomeiwen.com/subject/xplfdttx.html