• iOS截屏代码


    转载自:http://m.open-open.com/m/code/view/1420469506375

    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(@"截图成功!");  
          
            }  
        }  
    截全屏的代码:
    - (UIImage*)screenView:(UIView *)view{
    CGRect rect = view.frame;
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:context];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
    }
    如果需要导航栏也截取的话
    把[view.layer renderInContext:context]改成

    [self.navigationController.view.layer renderInContext:context]
     
  • 相关阅读:
    20162329张旭升 2017-2018-2 《程序设计与数据结构》第一周学习总结
    20162329 张旭升2016-2017《程序设计与数据结构》课程总结
    实验报告五
    20162329 张旭升 阶段四则运算(挑战出题)
    实验四:Android 开发基础
    四则运算的整体总结(第二周)
    结对编程四则运算(阶段总结)
    团队项目-选题报告
    第二次结对编程作业
    第一次结对编程作业
  • 原文地址:https://www.cnblogs.com/CodingMann/p/5231527.html
Copyright © 2020-2023  润新知