Gypsa
Gypsa

Reputation: 11314

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.

Upvotes: 1

Views: 1259

Answers (2)

Apurv
Apurv

Reputation: 17186

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

Upvotes: 0

Peter DeWeese
Peter DeWeese

Reputation: 18333

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

Upvotes: 1

Related Questions