Problem in writing metadata to image

后端 未结 3 1659
灰色年华
灰色年华 2021-01-01 06:19

I am using AvFoundation to take still image and adding gps info to metadata and saving to a photo album using Asset library but gps info is not saving at all.

here

3条回答
  •  醉话见心
    2021-01-01 06:56

    Mason's answer really helped me. You'll need some modifications such as setting the absolute value of longitude & latitude. Here's a code snippet of using CoreLocation + Image I/O to write an UIImage to disk with GPS information:

    - (BOOL)writeCGImage:(CGImageRef)theImage toURL:(NSURL*)url withType:(CFStringRef)imageType andOptions:(CFDictionaryRef)options {
        CGImageDestinationRef myImageDest   = CGImageDestinationCreateWithURL((CFURLRef)url, imageType, 1, nil);
        CGImageDestinationAddImage(myImageDest, theImage, options);
        BOOL success                        = CGImageDestinationFinalize(myImageDest);
    
        // Memory Mangement
        CFRelease(myImageDest);
        if (options)
            CFRelease(options);
    
        return success;
    }
    
    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
        if (newLocation) {
            [manager stopUpdatingLocation];
    
            // Create formatted date
            NSTimeZone      *timeZone   = [NSTimeZone timeZoneWithName:@"UTC"];
            NSDateFormatter *formatter  = [[NSDateFormatter alloc] init]; 
            [formatter setTimeZone:timeZone];
            [formatter setDateFormat:@"HH:mm:ss.SS"];
    
            // Create GPS Dictionary
            NSDictionary *gpsDict   = [NSDictionary dictionaryWithObjectsAndKeys:
                                         [NSNumber numberWithFloat:fabs(newLocation.coordinate.latitude)], kCGImagePropertyGPSLatitude
                                       , ((newLocation.coordinate.latitude >= 0) ? @"N" : @"S"), kCGImagePropertyGPSLatitudeRef
                                       , [NSNumber numberWithFloat:fabs(newLocation.coordinate.longitude)], kCGImagePropertyGPSLongitude
                                       , ((newLocation.coordinate.longitude >= 0) ? @"E" : @"W"), kCGImagePropertyGPSLongitudeRef
                                       , [formatter stringFromDate:[newLocation timestamp]], kCGImagePropertyGPSTimeStamp
                                       , nil];
    
            // Memory Management
            [formatter release];
    
            // Set GPS Dictionary to be part of media Metadata
            // NOTE: mediaInfo in this sample is dictionary object returned in UIImagePickerController delegate:
            // imagePickerController:didFinishPickingMediaWithInfo
            if (mediaInfo && [mediaInfo objectForKey:UIImagePickerControllerMediaMetadata] && gpsDict) {
                [[mediaInfo objectForKey:UIImagePickerControllerMediaMetadata] setValue:gpsDict forKey:@"{GPS}"];
            }
    
            // Save Image
            if([self writeCGImage:[image CGImage] toURL:imageSaveURL withType:kUTTypeJPEG andOptions:(CFDictionaryRef)[mediaInfo objectForKey:UIImagePickerControllerMediaMetadata]]) {
                // Image is written to device
            }
        } 
    }
    

提交回复
热议问题