新生成的图像质量低iOS迅速 [英] new generated image quality is low ios swift

查看:97
本文介绍了新生成的图像质量低iOS迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been making simple photo App using online tutorials. i made app that upload or capture image and can add text on image and save it. but the problem is saved image is very low quality. also it ha taken app buttons and bars to the output image too.

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

this is how i generated the final image

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)
    }

反正还有高质量的输出吗?

is there anyway to do this with quality output?

推荐答案

您没有调整宽高比,也需要注意调整视网膜大小

You are not resizing with aspect and you also need to take care about retina resize

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
}


Swift< 4

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);

这篇关于新生成的图像质量低iOS迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆