【问题标题】:new generated image quality is low ios swift新生成的图像质量低 ios swift
【发布时间】:2018-05-07 21:34:26
【问题描述】:

我一直在使用在线教程制作简单的照片应用程序。我制作了上传或捕获图像的应用程序,可以在图像上添加文本并保存它。但问题是保存的图像质量非常低。它也将应用程序按钮和条带到输出图像。

这就是我生成最终图像的方式

func save() {

        //Create the meme
        var meme = Meme( textField: topTextField.text!,textField2: bottomTextField.text!, image:
            imageView.image, memedImage: memedImage)
    }

func generateMemedImage() -> UIImage
    {
        // Render view to an image
        save()

        UIGraphicsBeginImageContext(self.view.frame.size)
        self.view.drawViewHierarchyInRect(self.view.frame,
            afterScreenUpdates: true)
        memedImage =
        UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return memedImage
    }

    @IBAction func saveImage(sender: AnyObject) {
        generateMemedImage()

        UIImageWriteToSavedPhotosAlbum(memedImage, nil, nil, nil)
    }

有没有办法用高质量的输出来做到这一点?

【问题讨论】:

    标签: ios swift uiimage


    【解决方案1】:

    您没有调整纵横比,您还需要注意视网膜调整大小

    斯威夫特 4

    func resizeImageWithAspect(image: UIImage,scaledToMaxWidth width:CGFloat,maxHeight height :CGFloat)->UIImage? {
        let oldWidth = image.size.width;
        let oldHeight = image.size.height;
        
        let scaleFactor = (oldWidth > oldHeight) ? width / oldWidth : height / oldHeight;
        
        let newHeight = oldHeight * scaleFactor;
        let newWidth = oldWidth * scaleFactor;
        let newSize = CGSize(width: newWidth, height: newHeight)
        
        UIGraphicsBeginImageContextWithOptions(newSize,false,UIScreen.main.scale);
        
        image.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height));
        let newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage
    }
    

    斯威夫特
    private func _resizeWithAspect_doResize(image: UIImage,size: CGSize)->UIImage{
        if UIScreen.mainScreen().respondsToSelector("scale"){
            UIGraphicsBeginImageContextWithOptions(size,false,UIScreen.mainScreen().scale);
        }
        else
        {
             UIGraphicsBeginImageContext(size);
        }
        
        image.drawInRect(CGRectMake(0, 0, size.width, size.height));
        var newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        return newImage;
    }
    
    func resizeImageWithAspect(image: UIImage,scaledToMaxWidth width:CGFloat,maxHeight height :CGFloat)->UIImage
    {
        let oldWidth = image.size.width;
        let oldHeight = image.size.height;
        
        let scaleFactor = (oldWidth > oldHeight) ? width / oldWidth : height / oldHeight;
        
        let newHeight = oldHeight * scaleFactor;
        let newWidth = oldWidth * scaleFactor;
        let newSize = CGSizeMake(newWidth, newHeight);
        
        return self._resizeWithAspect_doResize(image, size: newSize);
    }
    

    使用:

     var image : UIImage = yourImageClassInstance.resizeImageWithAspect(image:UIImage(named: "yourImg"),scaledToMaxWidth: 35.0, maxHeight: 45.0);
    

    【讨论】:

      猜你喜欢
      • 2021-10-27
      • 2015-11-23
      • 1970-01-01
      • 2012-05-13
      • 2021-06-16
      • 2014-10-14
      • 2019-08-23
      • 2014-12-24
      • 2017-08-03
      相关资源
      最近更新 更多