【问题标题】:UIGraphicsBeginImageContext with parametersUIGraphicsBeginImageContext 带参数
【发布时间】:2010-11-16 13:03:00
【问题描述】:

我正在我的应用程序中截取屏幕截图。我可以截图了。

现在我想通过指定 x 和 y 坐标来截取屏幕截图。这可能吗?

UIGraphicsBeginImageContext( self.view.bounds.size );
[self.view.layer renderInContext:UIGraphicsGetCurrentContext(  )];
UIImage* aImage = UIGraphicsGetImageFromCurrentImageContext(  );

【问题讨论】:

    标签: iphone iphone-sdk-3.0


    【解决方案1】:
    UIGraphicsBeginImageContext(self.view.bounds.size);
    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(c, 0, -40);    // <-- shift everything up by 40px when drawing.
    [self.view.layer renderInContext:c];
    UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    【讨论】:

    • @barbgal:使用 CGContextScaleCTM 调整大小。此外,您可能需要更小的上下文(将参数更改为 UIGraphicsBeginImageContext)。
    • @Barbgal:没有。CGSize size=self.view.bounds.size; UIGraphicsBeginImageContext(CGSizeMake(size.width,size.height-40)); CGContextRef c = UIGraphicsGetCurrentContext();
    【解决方案2】:

    如果您使用的是较新的 Retina 显示设备,您的代码应通过使用 UIGraphicsBeginImageContextWithOptions 而不是 UIGraphicsBeginImageContext 来考虑分辨率:

    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size,YES,2.0);
    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(c, 0, -40);    // <-- shift everything up by 40px when drawing.
    [self.view.layer renderInContext:c];
    UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    这将渲染视网膜显示图像上下文。

    【讨论】:

      【解决方案3】:

      做这样的事情,比所有那些复杂的计算要容易得多

      + (UIImage *)imageWithView:(UIView *)view {
          UIGraphicsBeginImageContextWithOptions([view bounds].size, NO, [[UIScreen mainScreen] scale]);
      
          [[view layer] renderInContext:UIGraphicsGetCurrentContext()];
      
          UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
      
          UIGraphicsEndImageContext();
      
          return result;
      }
      

      【讨论】:

        【解决方案4】:

        这里是 Swift 版本

        //Capture Screen
        func capture()->UIImage {
        
            UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, UIScreen.mainScreen().scale)
            self.view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return image
        
        }
        

        【讨论】:

          【解决方案5】:

          快捷版

              UIGraphicsBeginImageContext(self.view.bounds.size)
              let image: CGContextRef = UIGraphicsGetCurrentContext()!
              CGContextTranslateCTM(image, 0, -40)
              // <-- shift everything up by 40px when drawing.
              self.view.layer.renderInContext(image)
              let viewImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
              UIGraphicsEndImageContext()
              UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil)
          

          【讨论】:

            猜你喜欢
            • 2015-06-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-07-21
            • 1970-01-01
            相关资源
            最近更新 更多