我正在尝试将每次拍摄图像时从相机拍摄的图像保存到单独的相册中。现在使用我编写的代码保存图像但是进入共享相册,它不会保存到我想要保存的文件夹中,但会创建文件夹。此外,我希望每次拍摄图像时都要创建一个新文件夹,并将相册名称设置为“LiQu”,但我需要在从前一个视图控制器获取的文件夹旁边有一个ID号。 我上传了我对代码的尝试。
我正在尝试的视图控制器创建目录并将图像保存到。
import UIKit
import Photos
let albumName = "LiQu" // "+ID" i want to add the ID number from the previous view controller which is entered by the user
var transferImage: UIImage?
//var redlineX: Double = 0
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var albumFound : Bool = false
var assetCollection: PHAssetCollection!
var photosAsset: PHFetchResult<AnyObject>!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.init(red: 0.32, green: 0.667, blue: 0.754, alpha: 1)
//Check if LiQu album exists
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "title = %@", albumName)
let collection = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
if (collection.firstObject != nil){
// found the album
self.albumFound = true
self.assetCollection = collection.firstObject!
}else{
// create the album
PHPhotoLibrary.shared().performChanges({
//let request = PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: albumName)
PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: albumName)
})
}
}
我希望使用“ID”的视图控制器作为上面代码中目录的名称输入。
import UIKit
class FormViewController: UIViewController {
@IBOutlet weak var ID: UITextField!
@IBOutlet weak var organdonationno: UITextField!
@IBOutlet weak var donorage: UITextField!
@IBOutlet weak var snodname: UITextField!
@IBOutlet weak var donorshospital: UITextField!
@IBOutlet weak var date: UITextField!
我希望这是有道理的,所给予的任何帮助都非常感谢,我是新手,所以如果我犯了任何错误,请耐心等待。
THANKYOU
答案 0 :(得分:0)
您可以将ID从FormViewController传递给ViewController,并在创建相册时将ID附加到您的相册名称。在ViewController中将id定义为
var id : String = ""
将id从FormViewController传递给ViewController
let myVC = storyboard?.instantiateViewControllerWithIdentifier("ViewController ") as! ViewController
myVC.id= ID!
navigationController?.pushViewController(myVC, animated: true)
在创建新相册之前将ID添加到相册名称以将图像另存为。
albumName += id.
您可以参考this链接获取帮助,将图片保存到PHAssest中的自定义相册。