获取图片的 Metadata(元数据)

商飞航
2023-12-01
 
//利用 Assets Library Framework 且只有在调用image picker ,source为 UIImagePickerControllerSourceTypeCamera 的静态图像时才能获取元数据
 
 
- (void) imagePickerController: (UIImagePickerController *) picker
 didFinishPickingMediaWithInfo: (NSDictionary *) info {

    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
    UIImage *originalImage, *editedImage, *imageToSave;

    // Handle a still image capture
    if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0)
        == kCFCompareEqualTo) {

        editedImage = (UIImage *) [info objectForKey:
                                   UIImagePickerControllerEditedImage];
        originalImage = (UIImage *) [info objectForKey:
                                     UIImagePickerControllerOriginalImage];

        if (editedImage) {
            imageToSave = editedImage;
        } else {
            imageToSave = originalImage;
        }

        // Get the image metadata
        UIImagePickerControllerSourceType pickerType = picker.sourceType;
        if(pickerType == UIImagePickerControllerSourceTypeCamera)
        {
            NSDictionary *imageMetadata = [info objectForKey:
                                           UIImagePickerControllerMediaMetadata];
            // Get the assets library
            ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
            ALAssetsLibraryWriteImageCompletionBlock imageWriteCompletionBlock =
            ^(NSURL *newURL, NSError *error) {
                if (error) {
                    NSLog( @"Error writing image with metadata to Photo Library: %@", error );
                } else {
                    NSLog( @"Wrote image with metadata to Photo Library");
                }
            };

            // Save the new image (original or edited) to the Camera Roll
            [library writeImageToSavedPhotosAlbum:[imageToSave CGImage] 
                                         metadata:imageMetadata 
                                  completionBlock:imageWriteCompletionBlock];
        }
    }


    [[picker parentViewController] dismissModalViewControllerAnimated: YES];
    [picker release];
}
 
//获取相册中已存在的照片或视频的的元数据
// Get the assets library
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
                       usingBlock:^(ALAssetsGroup *group, BOOL *stop)
 {

     // Within the group enumeration block, filter to enumerate just photos.
     [group setAssetsFilter:[ALAssetsFilter allPhotos]];

     // For this example, we're only interested in the first item.
     [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:0]
                             options:0
                          usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop)
      {

          // The end of the enumeration is signaled by asset == nil.
          if (alAsset) {
              ALAssetRepresentation *representation = [alAsset defaultRepresentation];
              NSDictionary *imageMetadata = [representation metadata];
              // Do something interesting with the metadata.
          }
      }];
 }
                     failureBlock: ^(NSError *error)
 {
     // Typically you should handle an error more gracefully than this.
     NSLog(@"No groups");
 }];

[library release];

 类似资料: