往图片中写入GPS信息

方式一:

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];


        [library writeImageDataToSavedPhotosAlbum:processedJPEG metadata:stillCamera.currentCaptureMetadata completionBlock:^(NSURL *assetURL, NSError *error2)

        {

            if (error2) {

                NSLog(@"ERROR: the image failed to be written");

            }

            else {

                NSLog(@"PHOTO SAVED - assetURL: %@", assetURL);

            }

            runOnMainQueueWithoutDeadlocking(^{

                [photoCaptureButton setEnabled:YES];

            });

        }];

currentCaptureMetadata 里带上GPS信息

方式二:

NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:imageName], 1);

    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);

    NSDictionary *imageInfo = (__bridge NSDictionary*)CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);

    NSLog(@"%@",imageInfo);

    NSMutableDictionary *metaDataDic = [imageInfo mutableCopy];

    NSMutableDictionary *exifDic = [[metaDataDic objectForKey:(NSString*)kCGImagePropertyExifDictionary] mutableCopy];

    NSMutableDictionary *GPSDic = [[metaDataDic objectForKey:(NSString*)kCGImagePropertyGPSDictionary]mutableCopy];

    NSLog(@"%@",exifDic);

    NSLog(@"%@",GPSDic);

    if (GPSDic == nil) {

        GPSDic = [NSMutableDictionary dictionaryWithCapacity:0];

    }

    [GPSDic setObject:@"N" forKey:(NSString*)kCGImagePropertyGPSLatitudeRef];

    [GPSDic setObject:[NSNumber numberWithFloat:116.29353] forKey:(NSString*)kCGImagePropertyGPSLatitude];

    NSLog(@"%@",GPSDic);

    [metaDataDic setObject:GPSDic forKey:(NSString*)kCGImagePropertyGPSDictionary];

    CFStringRef UTI = CGImageSourceGetType(source);

    NSMutableData *newImageData = [NSMutableData data];

    CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)newImageData, UTI, 1,NULL);

    //add the image contained in the image source to the destination, overidding the old metadata with our modified metadata

    CGImageDestinationAddImageFromSource(destination, source, 0, (__bridge CFDictionaryRef)metaDataDic);

    CGImageDestinationFinalize(destination);

    NSString *directoryDocuments =  NSTemporaryDirectory();

    [newImageData writeToFile: directoryDocuments atomically:YES];

    CIImage *testImage = [CIImage imageWithData:newImageData];

    NSDictionary *propDict = [testImage properties];

    NSLog(@"Properties %@", propDict);

你可能感兴趣的:(往图片中写入GPS信息)