我一直在使用在线教程制作简单的照片应用程序.我创建了上传或捕获图像的应用程序,可以在图像上添加文本并保存.但问题是保存图像质量很差.它也采用了app按钮和条形图输出图像. 这
这就是我生成最终图像的方式
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) }
无论如何,有质量输出吗?
您没有调整方面的大小,您还需要注意视网膜调整大小斯威夫特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 }
斯威夫特< 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);