Reputation: 8986
I am trying to save image with the text label in the foreground of the image to photo library.I already worked out how to open the camera roll,taking picture and saving photo to the photo library.Currently, I am using screenshot function to capture the screen view.it works ok. But, I am not really satisfied with the result. Because,I have navigation bar and toolbar running in the same view.So,I have to use gesture recogniser function to hide both bars and long press gesture to save the screenshot to the photo library. It's looks like, I am doing too much coding to achieve a single function.I am looking for simple and effective solution.
My code below..
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.
}
}
Thanks in Advance...
Upvotes: 0
Views: 1310
Reputation: 8218
If I understood correctly, what you need is to add a text to an image and then save the result to the Photo Library. Is that it?
To add the text label to the image, you can use the Core Graphics framework. Here's a start:
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();
You'll probably need the UIKit Function Reference and the CGContext Reference as well.
Upvotes: 1