Lonergan6275 Lonergan6275 - 3 months ago 21
iOS Question

IOS Save image with name

i have an app that takes a picture and merges it with another image. I want to be able to display al the images taken in the app (and only my app) and display them in a gallery view. the problem I am having is in assigning the image a name.

I have tried a number of different ways

First Attempt

UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil)


this saved the image but i do not know how i can retrieve it

Second Attempt

PHPhotoLibrary.sharedPhotoLibrary().performChanges({
let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(newImage)

}, completionHandler: { (success, error) -> Void in
if success {
//image saved to photos library.
print("image saved")
}
});


Same Problem as first attempt

Third Attempt

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)


This seems to have the most promise but the file does bot save

I have looked at various answers on here but I still cant identify a solution

Answer

Add a slash as first character of the imageName string like this:

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

otherwise the path would be "..../Documentsmyimagename" but it should be "..../Documents/myimagename" i.e. without the additional slash the image is saved to the folder above "Documents"

I also recomend not to use the colon character in filenames, on a mac the colon is not allowed and is replaced automatically.

This is the code I tested in a new XCode project and that worked for me (I put a file "myimage.png" in the root folder of the project):

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)