2016-03-04 98 views
1

我有一個應用程序拍攝一張照片並將其與另一張圖像合併。我希望能夠顯示在應用程序(僅適用於我的應用程序)中拍攝的圖像並將其顯示在圖庫視圖中。我遇到的問題是分配圖像的名稱。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) 

這似乎也最有希望,但該文件確實BOT保存

我已經看過了各種答案就在這裏,但我仍然無法識別解決方案

+0

如果格式化在這樣的第三次嘗試的目錄路徑會發生什麼:'讓documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,真)[0]!字符串' – iCode

+0

@iCode它仍然不起作用。 – Lonergan6275

+0

保存時是否有錯誤? – iCode

回答

2

添加一個斜槓作爲imageName字符串的第一個字符,如下所示:

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

否則路徑將是「..../Documentsmyimagename」,但它應該是「.... /文檔/ myimagename」 即沒有將圖像保存到文件夾上面的‘文檔’

額外的斜槓

我也建議不要在文件名中使用冒號字符,在Mac上不允許使用冒號並自動替換。

這是我在一個新的XCode項目測試的代碼和工作對我來說(我把項目的根文件夾中的文件「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) 
+1

謝謝@iCode,但我已經嘗試了你的建議,雖然我錯過了/它顯然不是唯一的問題。文件路徑'/ var/mobile/Containers/Data/Application/long-app-id/Documents/2016-03-04_16-52-54.jpg' – Lonergan6275

+0

@ Lonergan6275我添加了適用於我的完整代碼(在Simulator中進行了測試) – iCode

+0

非常感謝,我將不得不回到它 – Lonergan6275

0

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)) 
    } 
} 
}