【问题标题】:Is it possible to add own metadata in captured Images in Swift是否可以在 Swift 中捕获的图像中添加自己的元数据
【发布时间】:2015-04-29 14:09:25
【问题描述】:

我对 Swift 和 Ios 编程非常陌生。如上所述,我喜欢将我自己的元数据插入到捕获的图像中,然后再将它们保存到相册。

我正在尝试使用此代码完成此操作。保存的图像不包含我自己的元数据,而是它生成的元数据。谁能告诉我我做错了什么?

或者是否可以将自己的新元数据表添加到捕获的图像中?

非常感谢您的帮助

@IBAction func btnPressed(sender: UIButton) {

    capturePicture()
}

func capturePicture(){

    stillImageOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
    session.addOutput(stillImageOutput)
    if let connection = self.stillImageOutput.connectionWithMediaType(AVMediaTypeVideo) {

        self.stillImageOutput.captureStillImageAsynchronouslyFromConnection(connection) {
            (imageDataSampleBuffer, error) -> Void in

            if error == nil {

                var asset = ALAssetsLibrary()
                let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)

                // The Metadata of the Image
                var metadata:NSDictionary = CMCopyDictionaryOfAttachments(nil, imageDataSampleBuffer, CMAttachmentMode(kCMAttachmentMode_ShouldPropagate)).takeUnretainedValue()
                // My Metadata i want to add for testing purpose
                var meta : NSDictionary = ["Ersteller": "Dennis","Datum" : "25.04.14","Ort" : "Köln" ]

                asset.writeImageDataToSavedPhotosAlbum(imageData, metadata: meta as [NSObject : AnyObject], completionBlock: { (path:NSURL!, error:NSError!) -> Void in
                    println("\(path)")
                    println("\(error)")
                })
            }
        }
    }
}

【问题讨论】:

    标签: ios swift uiimage avfoundation


    【解决方案1】:

    只需将下面的代码转换为 Swift。下面的代码是用 Objective-C 编写的。您只需要创建 IPTC 或 TIFF 字典。使用合适的 IPTC/TIFF 键增加价值并在图像上写入字典数据(元数据)。

      - (void) imagePickerController: (UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info
        {
    
            UIImage *image = info[UIImagePickerControllerOriginalImage];
    
            //Here We Get current system date and time and store as a description of photo
            NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"dd-MM-yyyy"];
            NSLog(@"Date Formatter : %@",[dateFormatter stringFromDate:[NSDate date]]);
    
            //hh:mm:ss
            NSDateFormatter *timeFormatter=[[NSDateFormatter alloc] init];
            [timeFormatter setDateFormat:@"hh:mm:ss"];
            NSLog(@"time Formatterr : %@",[timeFormatter stringFromDate:[NSDate date]]);
    
        //ADD IPTC Dictionary Data as a META DATA
            NSMutableDictionary *iptcDict = [NSMutableDictionary dictionary];
            [iptcDict setValue:[[DataEngine sharedInstance] getAlbumName] forKey:(NSString *)kCGImagePropertyIPTCObjectTypeReference]; //folder name
            [iptcDict setValue:@“Renish Dadhaniya - 101" forKey:(NSString *)kCGImagePropertyIPTCObjectAttributeReference]; //add Image ID -get using query from database
    
            [iptcDict setValue:[NSString stringWithFormat:@“Renish Sweet Memory "forKey:(NSString *)kCGImagePropertyIPTCObjectName]; //Add Image name
            [iptcDict setValue:[dateFormatter stringFromDate:[NSDate date]]forKey:(NSString *)kCGImagePropertyIPTCDateCreated]; //Add Image Date
            [iptcDict setValue:[timeFormatter stringFromDate:[NSDate date]]forKey:(NSString *)kCGImagePropertyIPTCTimeCreated]; //Add Image Time
    
            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            [dict setValue:iptcDict forKey:(NSString *)kCGImagePropertyIPTCDictionary];
    
            //Get Iamge Url
             __block NSURL *imageAssestURL = nil;
    
            [asSetLib writeImageToSavedPhotosAlbum:image.CGImage metadata:dict completionBlock:^(NSURL* assetURL, NSError* error) {
    
                if (error) {
                    NSLog(@"Image could not be safed to the assets library: %@", error);
                    imageAssestURL = nil;
                }
                else {
                    NSLog( @"Image safed successfully to assetURL: %@", assetURL);
                    imageAssestURL = assetURL;
    
                           }
    
            }];
    
            [picker dismissViewControllerAnimated:YES completion:nil];
    
        }
    

    【讨论】:

      猜你喜欢
      • 2021-02-01
      • 2021-09-16
      • 1970-01-01
      • 2017-07-14
      • 1970-01-01
      • 1970-01-01
      • 2017-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多