2014-06-19 26 views
10

我已经看到了其他职位这个代码,保存图片:如何将代码目标c转换为Swift以保存图像?

// Create path. 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES); 
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Image.png"]; 

// Save image. 
[UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES]; 

的d我想转换为迅速为保存图片采取与avfoundatioin但我不知道类型NSDocumentDirectory和NSUserDomainMask这里 哪有转换这个??

谢谢!

回答

19

如下:

let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory 
let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask 
if let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true) { 
    if paths.count > 0 { 
     if let dirPath = paths[0] as? String { 
      let readPath = dirPath.stringByAppendingPathComponent("Image.png") 
      let image = UIImage(named: readPath) 
      let writePath = dirPath.stringByAppendingPathComponent("Image2.png") 
      UIImagePNGRepresentation(image).writeToFile(writePath, atomically: true) 
     } 
    } 
} 

“路径”是一个AnyObject [],所以必须检查它的元素可以被转换成字符串。

当然,你不会真的使用“NSDocumentDirectory”作为名称,我只是为了清晰起见。现在

更新的Xcode 7.2

NSSearchPathForDirectoriesInDomains返回[String]而不是[AnyObject]?所以使用

let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory 
let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask 
let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true) 
if let dirPath = paths.first { 
    // ... 
} 

.stringByAppendingPathComponent也已经过时,被处理了this answer ...

+0

和其他行?因为对于filepath我可以选择这个:filePaths.stringByAppendingPathComponent(“Image.png”)但是如何选择索引0中的路径? – user3745888

+0

查看编辑答案。 – Grimxn

+0

如果我喜欢保存在我喜欢的路径中?可以写一个字符串的路径? – user3745888

0

用事实此类别保存文件目录中的任何图像

import Foundation 
import UIKit 
import ImageIO 
import MobileCoreServices 

extension UIImage { 
    func writeAtPath(path:String) -> Bool { 

     let result = CGImageWriteToFile(self.CGImage!, filePath: path) 
     return result 
    } 

    private func CGImageWriteToFile(image:CGImageRef, filePath:String) -> Bool { 
     let imageURL:CFURLRef = NSURL(fileURLWithPath: filePath) 
     var destination:CGImageDestinationRef? = nil 

     let ext = (filePath as NSString).pathExtension.uppercaseString 

     if ext == "JPG" || ext == "JPEG" { 
      destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeJPEG, 1, nil) 
     } else if ext == "PNG" || ext == "PNGF" { 
      destination = CGImageDestinationCreateWithURL(imageURL, kUTTypePNG, 1, nil) 
     } else if ext == "TIFF" || ext == "TIF" { 
      destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeTIFF, 1, nil) 
     } else if ext == "GIFF" || ext == "GIF" { 
      destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeGIF, 1, nil) 
     } else if ext == "PICT" || ext == "PIC" || ext == "PCT" || ext == "X-PICT" || ext == "X-MACPICT" { 
      destination = CGImageDestinationCreateWithURL(imageURL, kUTTypePICT, 1, nil) 
     } else if ext == "JP2" { 
      destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeJPEG2000, 1, nil) 
     } else if ext == "QTIF" || ext == "QIF" { 
      destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeQuickTimeImage, 1, nil) 
     } else if ext == "ICNS" { 
      destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeAppleICNS, 1, nil) 
     } else if ext == "BMPF" || ext == "BMP" { 
      destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeBMP, 1, nil) 
     } else if ext == "ICO" { 
      destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeICO, 1, nil) 
     } else { 
      fatalError("Did not find any matching path extension to store the image") 
     } 

     if (destination == nil) { 
      fatalError("Did not find any matching path extension to store the image") 
      return false 
     } else { 
      CGImageDestinationAddImage(destination!, image, nil) 

      if CGImageDestinationFinalize(destination!) { 
       return false 
      } 
      return true 
     } 
    } 
} 

//这是如何在你的应用程序中使用这个类别。

func testImageWrite() -> Bool { 

    let img = UIImage(named: "test") 

    var path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] 
    path = (path as NSString).stringByAppendingPathComponent("Test.png") 

    let result = img?.writeAtPath(path) 

    return result! 
} 
+0

https://gist.github.com/proactive-solutions/b265d6f2270f49126ce0 –

1

这里是我使用:

let fileManager = NSFileManager.defaultManager() 
    let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory 
    let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask 
    let documentsURL = fileManager.URLsForDirectory(nsDocumentDirectory, inDomains: nsUserDomainMask).last 
0

有关的Xcode 8.1,SWIFT 3.0,你可以这样做:

let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!

我想这是很多清洁和更短。

0

这仅仅是一个@返工पवन回答工作斯威夫特3

import Foundation 
import UIKit 
import ImageIO 
import MobileCoreServices 

extension UIImage 
{ 
    func write(at path:String) -> Bool 
    { 
     let result = CGImageWriteToFile(image: self.cgImage!, filePath: path) 
     return result 
    } 

    private func CGImageWriteToFile(image:CGImage, filePath:String) -> Bool 
    { 
     let imageURL:CFURL = NSURL(fileURLWithPath: filePath) 
     var destination:CGImageDestination? = nil 

     let ext = (filePath as NSString).pathExtension.lowercased() 
     if ext == "jpg" || ext == "jpeg" 
     { 
      destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeJPEG, 1, nil) 
     } 
     else if ext == "png" || ext == "pngf" 
     { 
      destination = CGImageDestinationCreateWithURL(imageURL, kUTTypePNG, 1, nil) 
     } 
     else if ext == "tiff" || ext == "tif" 
     { 
      destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeTIFF, 1, nil) 
     } 
     else if ext == "bmpf" || ext == "bmp" 
     { 
      destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeBMP, 1, nil) 
     } 


     guard destination != nil else { 
      fatalError("Did not find any matching path extension to store the image") 
     } 

     CGImageDestinationAddImage(destination!, image, nil) 

     if CGImageDestinationFinalize(destination!) 
     { 
      return true 
     } 
     return false 
    } 
} 

和使用

let pickedImage = UIImage() 
let path = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! as NSString).appendingPathComponent("image.tif") 
if pickedImage.write(at: path) == false 
{ 
    print("failed to write file to disk!") 
}