IOS用名字保存图像

我有一个应用程序,拍摄一张照片,并与另一个图像合并。 我希望能够显示在应用程序(和只有我的应用程序)拍摄的图像,并显示在画廊视图。 我遇到的问题是分配图像的名称。

我尝试了很多不同的方法

第一次尝试

UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil) 

这保存了图像,但我不知道如何检索它

第二次尝试

 PHPhotoLibrary.sharedPhotoLibrary().performChanges({ let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(newImage) }, completionHandler: { (success, error) -> Void in if success { //image saved to photos library. print("image saved") } }); 

与第一次尝试相同的问题

第三次尝试

 let date :NSDate = NSDate() let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd'_'HH:mm:ss" dateFormatter.timeZone = NSTimeZone(name: "GMT") let imageName = "\(dateFormatter.stringFromDate(date)).jpg" //var paths: [AnyObject] = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true) var documentsDirectoryPath = getDocumentsURL().relativePath documentsDirectoryPath! += imageName let settingsData: NSData = UIImageJPEGRepresentation(newImage, 1.0)! settingsData.writeToFile(documentsDirectoryPath!, atomically: true) 

这似乎是最有前途的,但文件没有保存

我已经看了这里的各种答案,但我仍然无法find一个解决scheme

像这样添加一个斜线作为imageNamestring的第一个字符:

 let imageName = "/\(dateFormatter.stringFromDate(date)).jpg" 

否则path将是“…. / Documentsmyimagename”,但它应该是“…. / Documents / myimagename”,即没有额外的斜杠图像保存到“Doc​​uments”

我也build议不要在文件名中使用冒号字符,在Mac上冒号是不允许的,并自动replace。

这是我在一个新的XCode项目中testing的代码,并为我工作(我把一个文件“myimage.png”在项目的根文件夹中):

 let newImage = UIImage.init(named: "myimage") let date :NSDate = NSDate() let dateFormatter = NSDateFormatter() //dateFormatter.dateFormat = "yyyy-MM-dd'_'HH:mm:ss" dateFormatter.dateFormat = "yyyy-MM-dd'_'HH_mm_ss" dateFormatter.timeZone = NSTimeZone(name: "GMT") let imageName = "/\(dateFormatter.stringFromDate(date)).jpg" print(imageName) //var paths: [AnyObject] = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true) //var documentsDirectoryPath = getDocumentsURL().relativePath var documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] documentsDirectoryPath += imageName print(documentsDirectoryPath) let settingsData: NSData = UIImageJPEGRepresentation(newImage!, 1.0)! settingsData.writeToFile(documentsDirectoryPath, atomically: true) 

SWIFT 3 🙂

 import UIKit class ShowImageViewController: UIViewController { @IBOutlet weak var mainImageView: UIImageView! @IBOutlet weak var tempImageView: UIImageView! private var lastPoint = CGPoint.zero private var red: CGFloat = 0.0 private var green: CGFloat = 0.0 private var blue: CGFloat = 0.0 private var brushWidth: CGFloat = 10.0 private var opacity: CGFloat = 1.0 private var swiped = false override func viewDidLoad() { super.viewDidLoad() mainImageView.image = captureImages[selectedImageindex] tempImageView.frame = getImageRect(for: mainImageView) // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { swiped = false if let touch = touches.first { lastPoint = touch.location(in: tempImageView) } } override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { swiped = true if let touch = touches.first { let currentPoint = touch.location(in: tempImageView) drawLineFrom(fromPoint: lastPoint, toPoint: currentPoint) lastPoint = currentPoint } } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { if !swiped { drawLineFrom(fromPoint: lastPoint, toPoint: lastPoint) } // Merge tempImageView into mainImageView UIGraphicsBeginImageContext(mainImageView.frame.size) mainImageView.image?.draw(in: CGRect(x: 0, y: 0, width: mainImageView.frame.size.width, height: mainImageView.frame.size.height), blendMode: .normal, alpha: 1.0) tempImageView.image?.draw(in: CGRect(x: 0, y: 0, width: mainImageView.frame.size.width, height: mainImageView.frame.size.height), blendMode: .normal, alpha: opacity) mainImageView.image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() tempImageView.image = nil } func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) { UIGraphicsBeginImageContext(mainImageView.frame.size) let context = UIGraphicsGetCurrentContext() tempImageView.image?.draw(in: CGRect(x: 0, y: 0, width: mainImageView.frame.size.width, height: mainImageView.frame.size.height)) context!.move(to: CGPoint(x: fromPoint.x, y: fromPoint.y)) context!.addLine(to: CGPoint(x: toPoint.x, y: toPoint.y)) context!.setLineCap(.round) context!.setLineWidth(brushWidth) context!.setStrokeColor(red: red, green: green, blue: blue, alpha: 1.0) context!.setBlendMode(.normal) context!.strokePath() tempImageView.image = UIGraphicsGetImageFromCurrentImageContext() tempImageView.alpha = opacity UIGraphicsEndImageContext() } func getImageRect(for imageView: UIImageView) -> CGRect { let resVi = (imageView.image?.size.width)! / (imageView.image?.size.height)! let resPl = imageView.frame.size.width / imageView.frame.size.height if resPl > resVi { let imageSize = CGSize(width: CGFloat((imageView.image?.size.width)! * imageView.frame.size.height / (imageView.image?.size.height)!), height: CGFloat(imageView.frame.size.height)) return CGRect(x: CGFloat((imageView.frame.size.width - imageSize.width) / 2), y: CGFloat((imageView.frame.size.height - imageSize.height) / 2), width: CGFloat(imageSize.width), height: CGFloat(imageSize.height)) } else { let imageSize = CGSize(width: CGFloat(imageView.frame.size.width), height: CGFloat((imageView.image?.size.height)! * imageView.frame.size.width / (imageView.image?.size.width)!)) return CGRect(x: CGFloat((imageView.frame.size.width - imageSize.width) / 2), y: CGFloat((imageView.frame.size.height - imageSize.height) / 2), width: CGFloat(imageSize.width), height: CGFloat(imageSize.height)) } } }