IOS 屏幕截图

来源:互联网 发布:mysql批量更新数据手动 编辑:程序博客网 时间:2024/05/17 06:24
   //截屏代码
    UIGraphicsBeginImageContext(CGSizeMake(1024, MAIN_SCREEN_HITHT));//设置截屏尺寸
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    image就是截取后的图片



另一种

  1.    //截图  
  2. UIGraphicsBeginImageContextWithOptions(CGSizeMake(640, 960), YES, 0);  
  3. [self.layer renderInContext:UIGraphicsGetCurrentContext()];  
  4. UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();  
  5. UIGraphicsEndImageContext();  
  6. CGImageRef imageRef = viewImage.CGImage;  
  7. CGRect rect =CGRectMake(100, 100, 200, 300);//这里可以设置想要截图的区域  
  8. CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);  
  9. UIImage *sendImage = [[UIImage alloc] initWithCGImage:imageRefRect];  
  10. NSData *imageViewData = UIImagePNGRepresentation(sendImage);  
  11.   
  12. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  13. NSString *documentsDirectory = [paths objectAtIndex:0];  
  14. NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"result.png"];  
  15.    NSLog(@"%@", savedImagePath);  
  16. [imageViewData writeToFile:savedImagePath atomically:YES];  
  17. CGImageRelease(imageRefRect);  

0 0
原创粉丝点击