2015-09-25 102 views
1

我想從與AVFoundation拍攝的照片,我怎樣才能迅速(2)首選做到這一點擺脫EXIF的數據,Objective-C的還好過,我知道如何轉換代碼迅速。Swift:如何從使用AVFoundation拍攝的照片中刪除EXIF數據?

爲什麼? 我已經完成了我的研究,並且看到很多着名的Social Media(Reddit源和更多)爲了身份識別和其他目的刪除EXIF數據。

如果您認爲這是重複的帖子,請閱讀我的要求並提供鏈接。謝謝。

回答

7

我的回答基於很多this previous question。我修改了Swift 2.0的代碼。

class ImageHelper { 
    static func removeExifData(data: NSData) -> NSData? { 
    guard let source = CGImageSourceCreateWithData(data, nil) else { 
     return nil 
    } 
    guard let type = CGImageSourceGetType(source) else { 
     return nil 
    } 
    let count = CGImageSourceGetCount(source) 
    let mutableData = NSMutableData(data: data) 
    guard let destination = CGImageDestinationCreateWithData(mutableData, type, count, nil) else { 
     return nil 
    } 
    // Check the keys for what you need to remove 
    // As per documentation, if you need a key removed, assign it kCFNull 
    let removeExifProperties: CFDictionary = [String(kCGImagePropertyExifDictionary) : kCFNull, String(kCGImagePropertyOrientation): kCFNull] 

    for i in 0..<count { 
     CGImageDestinationAddImageFromSource(destination, source, i, removeExifProperties) 
    } 

    guard CGImageDestinationFinalize(destination) else { 
     return nil 
    } 

    return mutableData; 
    } 
} 

然後,你可以簡單地做這樣的事情:

let imageData = ImageHelper.removeExifData(UIImagePNGRepresentation(image)) 

在我的例子,我刪除了旋轉和EXIF數據。如果您需要刪除其他任何內容,您可以輕鬆搜索密鑰。只需對生成的數據進行額外檢查,因爲它是可選的。

+0

你將需要進口的ImageIO在ImageHelper類 – Xitcod13

+0

閥塊這段代碼斯威夫特3並設置kcfNull不會刪除一個屬性,但例如我可以通過設置'kCGImagePropertyOrientation'作爲字符串:1'來將其更改爲其他內容,並且結果的方向值爲3. 但是我想刪除任何exif數據,因爲kcfNull沒有任何想法加工? –

+0

@MilosZikic你檢查過文檔了嗎?有一段時間沒有工作,但我知道它是寫在文檔中如何設置或刪除值。 –

0

你有UIImage嗎? 然後你可以的UIImage轉換成數據並保存到圖像再次新的圖像不會有任何的EXIF數據

斯威夫特3

let imageData:Data = UIImagePNGRepresentation(image!)! 



func saveToPhotoLibrary_iOS9(data:NSData, completionHandler: @escaping (PHAsset?)->()) { 
    var assetIdentifier: String? 
    PHPhotoLibrary.requestAuthorization { (status:PHAuthorizationStatus) in 
     if(status == PHAuthorizationStatus.authorized){ 

      PHPhotoLibrary.shared().performChanges({ 
       let creationRequest = PHAssetCreationRequest.forAsset() 
       let placeholder = creationRequest.placeholderForCreatedAsset 

       creationRequest.addResource(with: PHAssetResourceType.photo, data: data as Data, options: nil) 
       assetIdentifier = placeholder?.localIdentifier 

      }, completionHandler: { (success, error) in 
       if let error = error { 
        print("There was an error saving to the photo library: \(error)") 
       } 

       var asset: PHAsset? = nil 
       if let assetIdentifier = assetIdentifier{ 
        asset = PHAsset.fetchAssets(withLocalIdentifiers: [assetIdentifier], options: nil).firstObject//fetchAssetsWithLocalIdentifiers([assetIdentifier], options: nil).firstObject as? PHAsset 
       } 
       completionHandler(asset) 
      }) 
     }else { 
      print("Need authorisation to write to the photo library") 
      completionHandler(nil) 
     } 
    } 

}