简体   繁体   中英

Adding MetaData to my image and saving it in documents directory

My requirement is to set a description of the image in the meta data and save the image in documents directory.

I found how to create meta data and meta data is getting set if I save my image to photo library but I want to save the image in the documents directory.

Please suggest how to add meta data to my image and save the image in directory.

My Code :

 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);
        }
    }];

Any suggestions would be highly appreciated. Thanks in advance.

Many apps will use ALAssets and then use the resulting URL from the completion block to do additional operations. When this isn't appropriate, you'll need to write the data more manually. libexif.sourceforge.net/docs.html may help with this, as will the following two SO questions:

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

Which steps through going from a CGImageSourceRef to a CGImageDestinationRef with metadata.

Problem setting exif data for an image

Try with below code:

 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);
      }
 }];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM