IOS用名字保存图像 [英] IOS Save image with name

查看:97
本文介绍了IOS用名字保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我已经尝试了许多不同的方式



第一次尝试


$ b

  UIImageWriteToSavedPhotosAlbum(newImage,nil,nil,nil)

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

第二次尝试

  PHPhotoLibrary.sharedPhotoLibrary()。performChanges({
let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(newImage)
$ b $,completionHandler:{(success,error) - >如果成功{b $ b // //将图片保存到photoshop中, b $ b print(image saved)
}
});

与第一次尝试相同的问题

第三次尝试

  let date:NSDate = NSDate()

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat =yyyy-MM-dd'_'HH:mm:ss
dateFormatter.timeZone = NSTimeZone(name:GMT)

让imageName =\(dateFormatter.stringFromDate(date)).jpg

// var paths:[AnyObject] = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,true)
var documentsDirectoryPath = getDocumentsURL()。relativePath $ b $ documentsDirectoryPath! + = imageName

让settingsData:NSData = UIImageJPEGRepresentation(newImage,1.0)!
settingsData.writeToFile(documentsDirectoryPath !,原子地:true)

这似乎是最答案但文件确实保存了

我已经看过这里的各种答案,但是我仍然不能确定一个解决方案。



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

否则路径会是.... / Documentsmyimagename,但它应该是.... / Documents / myimagename
即没有额外的斜杠图像保存到文件上面的文件夹

我也建议不要在文件名中使用冒号字符,在Mac上不允许使用冒号,并自动替换。



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

  let newImage = UIImage.init(命名为:myimage)


let date:NSDate = NSDate()

let dateFormatter = NSDateFormatter()
//dateFormatter.dateFormat =yyyy-MM-dd'_'HH:mm:ss
dateFormatter.dateFormat =yyyy-MM-dd'_'HH_mm_ss
$ b $ 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,原子:true)


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

解决方案

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)

这篇关于IOS用名字保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆