【问题标题】:UIImage screenshot loses its qualityUIImage 截图失去了它的质量
【发布时间】:2012-06-16 16:30:15
【问题描述】:

我正在合并两张图片,然后通过应用以下代码截取屏幕截图:

UIGraphicsBeginImageContext(size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
img_AddText=viewImage;

[dragView removeFromSuperview];
imgV_SelectedImg.image=nil;
imgV_SelectedImg.image=img_AddText;
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

问题在于,当最终图像失去其质量时,它会变得模糊。

【问题讨论】:

    标签: iphone objective-c ios xcode uiimage


    【解决方案1】:

    尝试使用 UIGraphicsBeginImageContext 的 withOptions 版本

    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    

    【讨论】:

      【解决方案2】:

      我得到了高质量和特定屏幕位置的快照。通过这段代码。

      -(UIImage *)takeScreenShot
      {
          CGRect grabRect;
          grabRect = CGRectMake(0,70,320,260);
          UIGraphicsBeginImageContextWithOptions(grabRect.size, self.view.opaque, 0.0);
          CGContextRef ctx = UIGraphicsGetCurrentContext();
          CGContextTranslateCTM(ctx, -grabRect.origin.x, -grabRect.origin.y);
          [self.view.layer renderInContext:ctx];
          UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
          UIGraphicsEndImageContext();
      
          return viewImage;
      }
      

      它给了我很好的快照..

      【讨论】:

        【解决方案3】:

        我在UIImage 课程上创建了一个类别,可能会对您有所帮助。它是这样的:

        + (UIImage*)imageWithView:(UIView *)view opaque:(BOOL)opaque bgColor:(UIColor*)bgColor{
            UIGraphicsBeginImageContextWithOptions(view.bounds.size, opaque, [[UIScreen mainScreen] scale]);
        
            if(!opaque){
                [bgColor set];
            }
            [view.layer renderInContext:UIGraphicsGetCurrentContext()];
            UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            return img;
        }
        

        它对我来说很好用。未检测到模糊。尝试使用它。如果您仍然拥有它,那么很可能问题出在您的保存代码中......

        干杯... :)

        【讨论】:

        • 你的图片没有丢失质量的原因是你使用UIGraphicsBeginImageContextWithOptions(size, opaque, scale)就像Dcritellianswer一样
        【解决方案4】:

        UIGraphicsBeginImageContextWithOptions(size, NO, 2.0); 这通过将规模从 1.0 增加到 2.0 来解决我的问题

        【讨论】:

        • 传递 0 以使用设备分辨率 - 未来证明自己并保持兼容性。
        【解决方案5】:

        您是否提供了用于视网膜显示的图像?你应该检查一下。您可能正在模拟器中运行(在视网膜中)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-04-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多