【问题标题】:Adding MetaData to my image and saving it in documents directory将元数据添加到我的图像并将其保存在文档目录中
【发布时间】:2014-07-20 03:07:52
【问题描述】:

我的要求是在元数据中设置图片的描述,并将图片保存在文档目录中。

如果我将图像保存到照片库但我想将图像保存在文档目录中,我发现了如何创建元数据和元数据。

请建议如何将元数据添加到我的图像并将图像保存在目录中。

我的代码:

 NSMutableDictionary *tiffMetadata = [[NSMutableDictionary alloc] init];
    [tiffMetadata setObject:@"This is my description" forKey:(NSString*)kCGImagePropertyTIFFImageDescription];
    NSMutableDictionary *metadata = [[NSMutableDictionary alloc] init];
    [metadata setObject:tiffMetadata forKey:(NSString*)kCGImagePropertyTIFFDictionary];
    ALAssetsLibrary *assetsLib = [[ALAssetsLibrary alloc] init];
    NSData *imgData=[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"IMG_0015" ofType:@"jpg"]];
    [assetsLib writeImageDataToSavedPhotosAlbum:imgData metadata:metadata completionBlock:^(NSURL *assetURL, NSError *error) {
        if (error) {
            NSLog(@"%@",error.localizedDescription);
        }
    }];

任何建议将不胜感激。 提前致谢。

【问题讨论】:

    标签: ios objective-c metadata


    【解决方案1】:

    许多应用会使用 ALAsset,然后使用完成块生成的 URL 来执行其他操作。当这不合适时,您将需要更多地手动写入数据。 libexif.sourceforge.net/docs.html 可能会对此有所帮助,以下两个 SO 问题也会有所帮助:

    How to write exif metadata to an image (not the camera roll, just a UIImage or JPEG)

    从 CGImageSourceRef 到带有元数据的 CGImageDestinationRef 的步骤。

    Problem setting exif data for an image

    【讨论】:

      【解决方案2】:

      试试下面的代码:

       NSMutableDictionary *tiffMetadata = [[NSMutableDictionary alloc] init];
       [tiffMetadata setObject:@"This is my description" forKey:(NSString*)kCGImagePropertyTIFFImageDescription];
       NSMutableDictionary *metadata = [[NSMutableDictionary alloc] init];
       [metadata setObject:tiffMetadata forKey:(NSString*)kCGImagePropertyTIFFDictionary];
       ALAssetsLibrary *assetsLib = [[ALAssetsLibrary alloc] init];
       NSData *imgData=[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"IMG_0015" ofType:@"jpg"]];
       [assetsLib writeImageDataToSavedPhotosAlbum:imgData metadata:metadata completionBlock:^(NSURL *assetURL, NSError *error) {
            if(error == nil)
            {
                 NSFileManager *fileManager = [NSFileManager defaultManager];
                 NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                 NSString *docDirPath = [array objectAtIndex:0];
                 NSURL *destURL = [[NSURL alloc]initFileURLWithPath:docDirPath];
      
                 [fileManager copyItemAtURL:assetURL toURL:destURL error:nil];
      
            }
            else {
                 NSLog(@"%@",error.localizedDescription);
            }
       }];
      

      【讨论】:

      • 我试过了,但是 [fileManager copyItemAtURL:assetURL toURL:destURL error:nil];图像未保存到应用程序文件夹。请帮忙。
      猜你喜欢
      • 1970-01-01
      • 2015-12-05
      • 2012-10-12
      • 1970-01-01
      • 2012-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-13
      相关资源
      最近更新 更多