IOS使用名称保存图像

IOS使用名称保存图像,ios,swift,image,file,save,Ios,Swift,Image,File,Save,我有一个应用程序,可以拍摄一张照片并将其与另一张图像合并。我希望能够显示应用程序中拍摄的所有图像(仅我的应用程序),并将其显示在gallery视图中。我遇到的问题是为图像指定一个名称 我尝试了许多不同的方法 第一次尝试 UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil) PHPhotoLibrary.sharedPhotoLibrary().performChanges({ let assetRequest = PHAsset

我有一个应用程序,可以拍摄一张照片并将其与另一张图像合并。我希望能够显示应用程序中拍摄的所有图像(仅我的应用程序),并将其显示在gallery视图中。我遇到的问题是为图像指定一个名称

我尝试了许多不同的方法

第一次尝试

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)
这保存了图像,但我不知道如何检索它

第二次尝试

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)
与第一次尝试相同的问题

第三次尝试

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)
这似乎最有希望,但文件不会保存


我在这里查看了各种答案,但仍然无法确定解决方案

在imageName字符串的第一个字符中添加斜杠,如下所示:

let imageName = "/\(dateFormatter.stringFromDate(date)).jpg"
否则路径将是“…/Documentsmyimagename”,但它应该是“…/Documents/myimagename” i、 e.没有附加斜杠,图像保存到“文档”上方的文件夹中

我还建议不要在文件名中使用冒号字符,在mac上,冒号是不允许的,并且会自动替换

这是我在一个新的XCode项目中测试的代码,对我来说很有用(我在项目的根文件夹中放了一个文件“myimage.png”):

SWIFT 3:)

导入UIKit
类ShowImageViewController:UIViewController{
@IBVAR mainImageView:UIImageView!
@IBVAR tempImageView:UIImageView!
私有变量lastPoint=CGPoint.zero
私有变量红色:CGFloat=0.0
私有变量绿色:CGFloat=0.0
私有变量蓝色:CGFloat=0.0
私有变量刷宽度:CGFloat=10.0
私有变量不透明度:CGFloat=1.0
私有var swiped=false
重写func viewDidLoad(){
super.viewDidLoad()
mainImageView.image=captureImages[选择图像索引]
tempImageView.frame=getImageRect(用于:mainImageView)
//加载视图后执行任何其他设置。
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
刷卡=错误
如果让触摸=先触摸{
lastPoint=触摸位置(在:tempImageView中)
}
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
刷卡=正确
如果让触摸=先触摸{
让currentPoint=touch.location(在:tempImageView中)
drawLineFrom(起点:最后一点,拓扑点:当前点)
lastPoint=currentPoint
}
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
如果!刷卡{
drawLineFrom(起点:最后一点,地形点:最后一点)
}
//将tempImageView合并到mainImageView中
UIGraphicsBeginImageContext(mainImageView.frame.size)
mainImageView.image?.draw(in:CGRect(x:0,y:0,宽度:mainImageView.frame.size.width,高度:mainImageView.frame.size.height),blendMode:.normal,alpha:1.0)
tempImageView.image?.draw(in:CGRect(x:0,y:0,宽度:mainImageView.frame.size.width,高度:mainImageView.frame.size.height),blendMode:.normal,alpha:opacity)
mainImageView.image=UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsSendImageContext()
tempImageView.image=nil
}
func drawLineFrom(起点:CGPoint,地形点:CGPoint){
UIGraphicsBeginImageContext(mainImageView.frame.size)
let context=UIGraphicsGetCurrentContext()
tempImageView.image?.draw(in:CGRect(x:0,y:0,宽度:mainImageView.frame.size.width,高度:mainImageView.frame.size.height))
上下文!.move(移动到:CGPoint(x:fromPoint.x,y:fromPoint.y))
context!.addLine(到:CGPoint(x:toPoint.x,y:toPoint.y))
上下文!.setLineCap(.round)
上下文!.setLineWidth(笔刷宽度)
context!.setStrokeColor(红色:红色,绿色:绿色,蓝色:蓝色,alpha:1.0)
上下文模式(.normal)
上下文!.strokePath()
tempImageView.image=UIGraphicsGetImageFromCurrentImageContext()
tempImageView.alpha=不透明度
UIGraphicsSendImageContext()
}
func getImageRect(对于imageView:UIImageView)->cRect{
让resVi=(imageView.image?size.width)!/(imageView.image?size.height)!
设resPl=imageView.frame.size.width/imageView.frame.size.height
如果resPl>resVi{
设imageSize=CGSize(宽度:CGFloat((imageView.image?size.width)!*imageView.frame.size.height/(imageView.image?size.height)!),高度:CGFloat(imageView.frame.size.height))
返回CGRect(x:CGFloat((imageView.frame.size.width-imageSize.width)/2),y:CGFloat((imageView.frame.size.height-imageSize.height)/2),宽度:CGFloat(imageSize.width),高度:CGFloat(imageSize.height))
}
否则{
设imageSize=CGSize(宽度:CGFloat(imageView.frame.size.width),高度:CGFloat((imageView.image?size.height)!*imageView.frame.size.width/(imageView.image?size.width)!)
返回CGRect(x:CGFloat((imageView.frame.size.width-imageSize.width)/2),y:CGFloat((imageView.frame.size.height-imageSize.height)/2),宽度:CGFloat(imageSize.width),高度:CGFloat(imageSize.height))
}
}
}

如果第三次尝试将目录路径格式化为以下格式,会发生什么情况:
将documentsDirectoryPath=NSSearchPathForDirectoriesInDomains(.DocumentDirectory、.UserDomainMask,true)[0]设置为!String
@iCode它仍然不起作用。保存时您是否有任何错误?@iCode不幸的是,没有任何错误@iCode,但我尝试了您的建议,虽然我错过了/It显然不是唯一的问题。文件路径
/var/mobile/Containers/Data/Application/long-app id/Documents/2016-03-04_16-52-54.jpg
@Lonergan6275我添加了对我有用的完整代码(在模拟器中测试),非常感谢我将不得不返回itIt。它已保存。但是,我怎么看呢?我找不到它。@gkhanacer模拟器的所有文件都在~/Library/Developer/CoreSimulator/Devices/(每个模拟iPhone/iPad都有一个文件夹)。但在Finder中搜索图像名称更容易(确保系统文件