如何保存照片和文字

时间:2015-08-14 08:11:44

标签: ios swift uiimageview

我有一张图片和一张文字。如果用户点击safeButton,我想保存它。

@IBOutlet weak var img1: UIImageView!
@IBOutlet weak var text1: UITextField!
@IBAction func saveButton(sender: AnyObject) {
    save()
}

我可以使用func快照保护视图。但是如何保存文本,img和快照?

我尝试了func save。我在func中有一个错误:“使用未解析的标识符对象”“使用未解析的标识符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
}

2 个答案:

答案 0 :(得分:1)

以下是这样做的方法:

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
    }
}

结果将是:

enter image description here

答案 1 :(得分:0)

您可以通过两种方式执行此操作:

1-将照片保存在临时目录中。例如:

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-使用Photos Framework保存。例如:

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!"))
                        })
                })

            })
        }

您可以查看project sample

取自:https://stackoverflow.com/a/32015320/3273221