Reputation: 9787
I have an image and a text. If the user clicks to the safeButton I would like to save it.
@IBOutlet weak var img1: UIImageView!
@IBOutlet weak var text1: UITextField!
@IBAction func saveButton(sender: AnyObject) {
save()
}
I could safe the view with the func snapshot. But how can I save the text, the img and the snapshot?
I tried the func save. I have an error in the func save: "use of unresolved identifier Object" "use of unresolved identifier imageView"
func save() {
var object = Object( text: textField.text!, image:
imageView.image, image: image)
}
func snapshot() -> UIImage {
UIGraphicsBeginImageContext(self.view.frame.size)
self.view.drawViewHierarchyInRect(self.view.frame,
afterScreenUpdates: true)
let image : UIImage =
UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
Upvotes: 2
Views: 261
Reputation: 36
You can do this in two ways:
1- Saving the photo in a temporary directory. Example:
var fileManager = NSFileManager()
var tmpDir = NSTemporaryDirectory()
let filename = "YourImageName.png"
let path = tmpDir.stringByAppendingPathComponent(filename)
var error: NSError?
let imageData = UIImagePNGRepresentation(YourImageView.image)
fileManager.removeItemAtPath(path, error: nil)
println(NSURL(fileURLWithPath: path))
if(imageData.writeToFile(path,atomically: true)){
println("Image saved.")
}else{
println("Image not saved.")
}
2- Saving using Photos Framework. Example:
if let image: UIImage = YourImageView.image
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0), {
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
let createAssetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
let assetPlaceholder = createAssetRequest.placeholderForCreatedAsset
if let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection, assets: self.photosAsset) {
albumChangeRequest.addAssets([assetPlaceholder])
}
}, completionHandler: {(success, error)in
dispatch_async(dispatch_get_main_queue(), {
NSLog("Adding Image to Library -> %@", (success ? "Sucess":"Error!"))
})
})
})
}
You can check this project sample.
Taken from: https://stackoverflow.com/a/32015320/3273221
Upvotes: 0
Reputation: 71854
Here is the way to do it:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var newImage: UIImageView!
@IBOutlet weak var img1: UIImageView!
@IBAction func saveIt(sender: AnyObject) {
//first make an UIImage from your view
UIGraphicsBeginImageContext(self.view.bounds.size)
self.view.layer.renderInContext(UIGraphicsGetCurrentContext())
let sourceImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
//draw new Image
//position the image
UIGraphicsBeginImageContext(img1.frame.size)
sourceImage.drawAtPoint(CGPointMake(0, 0))
let croppedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
//assign new Image
newImage.image = croppedImage
}
}
And result will be:
Upvotes: 1