iOS截屏代码

/**

 *截图功能

 */

-(void)screenShot{

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

    //设置截屏大小

    [[self.view layerrenderInContext:UIGraphicsGetCurrentContext()];

    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    CGImageRef imageRef = viewImage.CGImage;

    CGRect rect = CGRectMake(00641SCREEN_HEIGHT + 300);//这里可以设置想要截图的区域

    CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);

    UIImage *sendImage = [[UIImage allocinitWithCGImage:imageRefRect];

 

   //以下为图片保存代码

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

    NSData *imageViewData = UIImagePNGRepresentation(sendImage);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMaskYES);

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

    

}

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

/** *截图 */- (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(@"截图成功!");    }}
上一篇:【我的Android进阶之旅】Android 7.0报异常:java.lang.SecurityException: COLUMN_LOCAL_FILENAME is deprecated;


下一篇:细谈CSS布局方式