在临时位置保存带有GPS信息的iOS相机照片

时间:2016-04-04 14:11:58

标签: ios objective-c uiimagepickercontroller

我需要使用iOS设备相机拍照,然后将其上传到服务器。问题是,默认情况下,从UIImagePickerController信息中获取的UIImage不包含GPS元数据。

在此post之后,我已经能够使用正确的GPS信息将图像保存在Camerra Roll中。使用的代码如下:

- (void)saveImage:(UIImage *)imageToSave withInfo:(NSDictionary *)info {
    // Get the image metadata (EXIF & TIFF)
    NSMutableDictionary * imgMtd = [[info objectForKey:UIImagePickerControllerMediaMetadata] mutableCopy];

    // create CLLocation for image
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    CLLocation * loc = [locationManager location];

    if (loc) {
        [imgMtd setObject:[self gpsDictionaryForLocation:loc] forKey:(NSString*)kCGImagePropertyGPSDictionary];
    }

    // Get the assets library
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

    // create a completion block for when we process the image
    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",newURL,imgMtd);
        }
    };

    // Save the new image to the Camera Roll, using the completion block defined just above
    [library writeImageToSavedPhotosAlbum:[imageToSave CGImage] metadata:imgMtd completionBlock:imageWriteCompletionBlock];

}

但是,我正在使用此照片将其上传到服务器。因此,所需的行为是将其保存在时间路径(因此图像将无法在相机胶卷处可用),将其上载到服务器然后将其删除。使用提供的代码,我将图像公开,并且不知道上传它的路径。

0 个答案:

没有答案