我正在尝试将图像前景中的文本标签保存到照片库。我已经研究出如何打开相机胶卷,拍照并将照片保存到照片库。目前,我正在使用截图捕获屏幕视图的功能。工作正常。但是,我对结果并不满意。因为,我在相同的视图中运行导航栏和工具栏。因此,我必须使用手势识别器功能来隐藏条形和长按手势以将屏幕截图保存到照片库。看起来,我正在做太多编码才能实现单一功能。我正在寻找简单有效的解决方案。
我的代码如下..
import UIKit
import CoreGraphics
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
@IBOutlet var imageView: UIImageView!
@IBOutlet weak var label: UILabel!
var imagePicker: UIImagePickerController!
@IBAction func takePhoto(sender: UIButton) {
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .PhotoLibrary
imagePicker.modalPresentationStyle = .FullScreen
presentViewController(imagePicker, animated: true, completion: nil)
}
//Save Button
@IBAction func save(sender: AnyObject) {
let sourceImage = imageView.image
UIGraphicsBeginImageContext(sourceImage!.size);
let context:CGContextRef = UIGraphicsGetCurrentContext()!
let rect:CGRect = CGRectMake(0, 0, sourceImage!.size.width, sourceImage!.size.height);
CGContextDrawImage(context, rect, sourceImage!.CGImage);
// draw your text here
let resultImage: UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext()
UIImageWriteToSavedPhotosAlbum(imageView.image!, self, "image:didFinishSavingWithError:contextInfo:", nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
imagePicker.dismissViewControllerAnimated(true, completion: nil)
imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
label.text = label.text
}
func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
if error == nil {
let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(ac, animated: true, completion: nil)
} else {
let ac = UIAlertController(title: "Save error", message: error?.localizedDescription, preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(ac, animated: true, completion: nil)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
提前致谢...
答案 0 :(得分:1)
如果我理解正确,您需要的是向图像添加文本,然后将结果保存到照片库。是吗?
要将文本标签添加到图像,您可以使用Core Graphics framework。这是一个开始:
UIImage *sourceImage = ...
UIGraphicsBeginImageContext(sourceImage.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect rect = CGRectMake(0, 0, sourceImage.size.width, sourceImage.size.height);
CGContextDrawImage(context, rect, sourceImage.CGImage);
// draw your text here
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();