Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new generated image quality is low ios swift

Tags:

ios

swift

uiimage

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?

like image 575
Im Batman Avatar asked Dec 03 '22 16:12

Im Batman


1 Answers

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

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

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

use:

 var image : UIImage = yourImageClassInstance.resizeImageWithAspect(image:UIImage(named: "yourImg"),scaledToMaxWidth: 35.0, maxHeight: 45.0);
like image 125
Daniel Krom Avatar answered Dec 22 '22 00:12

Daniel Krom