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
}
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With