2016-05-09 3 views
1

Я пытаюсь добавить метаданные GPS к изображению, снятому с AVFoundation в быстром. У меня есть код, вставленный ниже, но он пока не работает. Где моя ошибка? Методдобавить словарь метаданных GPS в изображение, сделанное с AVFoundation in swift

let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer) 
let imgDataProvider = CGDataProviderCreateWithCFData(imageData); 
let imageRef = CGImageCreateWithJPEGDataProvider(imgDataProvider, nil, true, .RenderingIntentDefault) 
let properties = self.getImageProperties(imageData) 
let mutableDictionary: NSMutableDictionary = NSMutableDictionary(dictionary: properties) 
mutableDictionary.setValue(self.getGPSDictionary(), forKey: kCGImagePropertyGPSDictionary as String) 
let newImageData = CFDataCreateMutable(nil, 0) 
let type = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, "image/jpg" as CFString, kUTTypeImage) 
let destination = CGImageDestinationCreateWithData(newImageData, (type?.takeRetainedValue())!, 1, nil) 
CGImageDestinationAddImage(destination!, imageRef!, mutableDictionary) 
CGImageDestinationFinalize(destination!) 
let newImage = newImageData 

getImageProperties:

private func getImageProperties(data: NSData) -> NSDictionary { 
    let imageSourceRef = CGImageSourceCreateWithData(data, nil); 
    let properties = CGImageSourceCopyPropertiesAtIndex(imageSourceRef!, 0, nil) 
    let props = properties 
    return props! 
} 

getGPSDictionary метод:

private func getGPSDictionary() -> NSDictionary { 
    let dictionary = [kCGImagePropertyGPSLatitude as String :"54",kCGImagePropertyGPSLongitude as String :"23"] 
    return dictionary 
} 

Да, я знаю, что координаты зашиты до сих пор, но важно сейчас, чтобы сделать его работу , я обновлю местоположение.

ответ

0

Сохранить изображение через ALAssetsLibrary:

let imgDataProvider = CGDataProviderCreateWithCFData(imageData); 
let imageRef = CGImageCreateWithJPEGDataProvider(imgDataProvider, nil, true, .RenderingIntentDefault) 
let library = ALAssetsLibrary() 

library.writeImageToSavedPhotosAlbum(imageRef, metadata:mutableDictionary as [NSObject : AnyObject], completionBlock:nil) 
1

Это, как я это сделал. Это работало на Swift 2.2 IOS 9,2

@IBAction func shutter(sender: AnyObject) { 
    let connection = imageOutput.connectionWithMediaType(AVMediaTypeVideo) 

    // set the orientation 
    if connection.supportsVideoOrientation { 
     connection.videoOrientation = currentVideoOrientation() 
    } 

    imageOutput.captureStillImageAsynchronouslyFromConnection(connection) { 
     (sampleBuffer: CMSampleBuffer!, error: NSError!) -> Void in 
     if sampleBuffer != nil { 
      let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer) 

      // get the old EXIF 
      let oldEXIF = self.getEXIFFromImage(imageData) 

      // add gps info to the old exif 
      let newEXIF = self.addGPSToEXIF(oldEXIF, new: self.getGPSDictionaryForLocation(Genba.sharedInstance.gps.currentLocation)) 

      // resize the image to desired 
      let resized = self.resizeNSDataImage(imageData, newSize: Genba.sharedInstance.imageSize) 

      // attach the new exif to the resized image 
      let attached = self.attachEXIFtoImage(resized, EXIF: newEXIF) 

      // save the file to the camera roll and inside the app 
      self.saveFile(attached) 

     } else { 
      debugPrint(self.n,#line,"Error capturing photo \(error.localizedDescription)") 
     } 
    } 
} 

Вспомогательные функции здесь

// combine exif dictionaries 
func addGPSToEXIF(old:NSDictionary, new:NSDictionary) -> NSDictionary { 

    old.setValue(new, forKey: kCGImagePropertyGPSDictionary as String) 

    return old 
} 

// get an NSDictionary from an image 
func getEXIFFromImage(image:NSData) -> NSDictionary { 

    let imageSourceRef = CGImageSourceCreateWithData(image, nil); 

    let currentProperties = CGImageSourceCopyPropertiesAtIndex(imageSourceRef!, 0, nil) 

    let mutableDict = NSMutableDictionary(dictionary: currentProperties!) 

    return mutableDict 

} 

// Attach EXIF DIctionary data to an image 
func attachEXIFtoImage(image:NSData, EXIF:NSDictionary) -> NSData { 

    let imageDataProvider:CGDataProviderRef = CGDataProviderCreateWithCFData(image)! 

    let imageRef:CGImageRef = CGImageCreateWithJPEGDataProvider(imageDataProvider, nil, true, .RenderingIntentDefault)! 

    let newImageData:CFMutableDataRef = CFDataCreateMutable(nil, 0) 

    let type = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, "image/jpg" as CFString, kUTTypeImage) 

    let destination:CGImageDestinationRef = CGImageDestinationCreateWithData(newImageData, (type?.takeRetainedValue())!, 1, nil)! 

    CGImageDestinationAddImage(destination, imageRef, EXIF as CFDictionaryRef) 

    CGImageDestinationFinalize(destination) 

    return newImageData as NSData 

} 
// save file within app with EXIF 
func saveFile(image: NSData) { 
     // Saves the file out 
    let dataPath = Genba.sharedInstance.userdata_save_folder 
    let fileExists = dataPath.checkResourceIsReachableAndReturnError(nil) 
    if fileExists { 

     let formatter = NSDateFormatter() 
     formatter.dateFormat = "Y-M-dd_HH-mm-ss" 
     let time = formatter.stringFromDate(NSDate()) 

     let savefilepath = dataPath.URLByAppendingPathComponent(time+".jpg") 

     let result = image.writeToURL(savefilepath, atomically: false) 

     if result { 
      savePhotoToLibraryWithURL(savefilepath) 
     } else { 
      debugPrint(n,#line,"failed to save") 
     } 
    } 
} 
// resize image from NSData 
func resizeNSDataImage (inputImage:NSData, newSize:CGSize) -> NSData { 

    var smallerImage:NSData? = nil 

    let image = UIImage(data: inputImage) 

    UIGraphicsBeginImageContext(newSize) 

    image!.drawInRect(CGRect(origin: CGPointZero, size: newSize)) 

    let newImage = UIGraphicsGetImageFromCurrentImageContext() 

    UIGraphicsEndImageContext() 

    smallerImage = UIImageJPEGRepresentation(newImage, 1) 

    return smallerImage! 
} 

функ savePhotoToLibraryWithURL (изображение: NSURL) {

let photoLibrary = PHPhotoLibrary.sharedPhotoLibrary() 

    photoLibrary.performChanges({ 
     PHAssetChangeRequest.creationRequestForAssetFromImageAtFileURL(image) 
    }) { (success: Bool, error: NSError?) -> Void in 
     if success { 

     } else { 
      print(self.n,#line,"Error writing to photo library: \(error!.localizedDescription)") 

     } 
    } 
} 

 Смежные вопросы

  • Нет связанных вопросов^_^