iOS截屏代码

1.普通界面

/**
 *截图功能
 */
-(void)screenShot{
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(640, 960), YES, 0);

    //设置截屏大小

    [[self.view layer] renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    CGImageRef imageRef = viewImage.CGImage;
    CGRect rect = CGRectMake(0, 0, 641, SCREEN_HEIGHT + 300);//这里可以设置想要截图的区域

    CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);
    UIImage *sendImage = [[UIImage alloc] initWithCGImage:imageRefRect];
 

    //以下为图片保存代码

    UIImageWriteToSavedPhotosAlbum(sendImage, nil, nil, nil);//保存图片到照片库

    NSData *imageViewData = UIImagePNGRepresentation(sendImage);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pictureName= @"screenShow.png";
    NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:pictureName];
    [imageViewData writeToFile:savedImagePath atomically:YES];//保存照片到沙盒目录

    CGImageRelease(imageRefRect);    



    //从手机本地加载图片

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

    
}
2.UIScrollView截屏(一屏无法显示完整)

/**
 *截图
 */
- (void)screenShot{
    UIImage* image = nil;
    UIGraphicsBeginImageContext(m_scrollView.contentSize);

    {
        CGPoint savedContentOffset = m_scrollView.contentOffset;
        CGRect savedFrame = m_scrollView.frame;
        m_scrollView.contentOffset = CGPointZero;

        m_scrollView.frame = CGRectMake(0, 0, m_scrollView.contentSize.width, m_scrollView.contentSize.height);        [m_scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];

        image = UIGraphicsGetImageFromCurrentImageContext();     

        m_scrollView.contentOffset = savedContentOffset;
        m_scrollView.frame = savedFrame;
    }
    UIGraphicsEndImageContext();    

    if (image != nil) {
        NSLog(@"截图成功!");

    }
}


传送门: http://dandy-lan.blog.163.com/blog/static/14789598820143132547662/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值