ios - 如何通过Photokit保存图片(带有关于定位GPS的Metadata信息)到相册?
大家讲道理
大家讲道理 2017-04-17 16:50:55
0
2
1145

我通过以下2种方法将有关位置的metadata信息写入照片:

  1. 将位置信息写入image,让后调用系统api将图片保存到相册

- (void)writeImageDataToSavedPhotosAlbum:(NSData *)imageData metadata:(NSDictionary *)metadata resultBlock:(PHAssetAssetBoolBlock)resultBlock
{
    __block PHObjectPlaceholder *placeholderAsset = nil;
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        UIImage *image = [self imageWithData: imageData metadata:metadata];
        [PHAssetChangeRequest creationRequestForAssetFromImage: image];
        placeholderAsset = newAssetRequest.placeholderForCreatedAsset;
    } completionHandler:^(BOOL success, NSError *error) {
        if(success){
            PHAsset *asset = [self getAssetFromlocalIdentifier:placeholderAsset.localIdentifier];
            completionBlock(asset, YES);
        } else {
            completionBlock(nil, NO);
        }
    }];
}

- (UIImage *)imageWithData:(NSData *)imageData metadata:(NSDictionary *)metadata
{
    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
    NSMutableDictionary * sourceDic = [NSMutableDictionary dictionary];
    NSDictionary *source_metadata = (NSDictionary *)CFBridgingRelease(CGImageSourceCopyProperties(source, NULL));
    [sourceDic addEntriesFromDictionary: metadata];
    [sourceDic addEntriesFromDictionary:source_metadata];
    
    NSMutableData *dest_data = [NSMutableData data];
    CFStringRef UTI = CGImageSourceGetType(source);
    CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)dest_data, UTI, 1,NULL);
    CGImageDestinationAddImageFromSource(destination, source, 0, (__bridge CFDictionaryRef)sourceDic);
    CGImageDestinationFinalize(destination);
    CFRelease(source);
    CFRelease(destination);
    return [UIImage imageWithData: dest_data];
}

2.用PHAssetChangeRequest的location属性保存,代码如下:

    +(void)saveImageToCameraRoll:(UIImage*)image location:(CLLocation*)location completionBlock:(PHAssetAssetBoolBlock)completionBlock{
    __block PHObjectPlaceholder *placeholderAsset = nil;
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        PHAssetChangeRequest *newAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
        newAssetRequest.location = location;
        newAssetRequest.creationDate = [NSDate date];
        placeholderAsset = newAssetRequest.placeholderForCreatedAsset;
    } completionHandler:^(BOOL success, NSError *error) {
        if(success){
            PHAsset *asset = [self getAssetFromlocalIdentifier:placeholderAsset.localIdentifier];
            completionBlock(asset, YES);
        } else {
            completionBlock(nil, NO);
        }
    }];
}

然后获取照片的metadata信息,发现没有关于GPS的信息,获取方法如下:

- (void)getPhotoMetadataWithAsset:(PHAsset *)asset
{
    PHImageRequestOptions *request = [PHImageRequestOptions new];
    request.version = PHImageRequestOptionsVersionCurrent;
    request.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
    request.resizeMode = PHImageRequestOptionsResizeModeNone;
    request.synchronous = YES;
    
    [[PHImageManager defaultManager] requestImageDataForAsset:asset options: request resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
        CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
        if (NULL != source) {
            NSDictionary * metadataDic = (NSDictionary *)CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source, 0, NULL));
            NSLog(@"metada:%@",metadataDic);
            CFRelease(source);
        }
    }];
}

麻烦看下是保存metadata过程有问题?还是获取metadata有问题?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

모든 응답(2)
Peter_Zhu

답변을 찾았습니다. 추가해 주세요.

먼저 두 번째 방법으로 저장합니다. 얻는 것이 그렇게 번거로울 필요는 없습니다. 그냥 자산 위치를 직접 얻으세요.

+(void)saveImageToCameraRoll:(UIImage*)이미지 위치:(CLLocation*)위치 완료Block:(PHAssetAssetBoolBlock)completionBlock{
    __block PHObjectPlaceholder *placeholderAsset = nil;
    [[PHPhotoLibrary sharedPhotoLibrary] 수행변경:^{
        PHAssetChangeRequest *newAssetRequest = [PHAssetChangeRequest 생성요청ForAssetFromImage:이미지];
        newAssetRequest.location = 위치;
        newAssetRequest.creationDate = [NSDate 날짜];
        placeholderAsset = newAssetRequest.placeholderForCreatedAsset;
    } CompletionHandler:^(BOOL 성공, NSError *error) {
        if(성공){
            PHAsset *asset = [self getAssetFromlocalIdentifier:placeholderAsset.localIdentifier];
            CLLocation * 위치 = 자산.위치;
            CompletionBlock(자산, YES);
        } 또 다른 {
            CompletionBlock(nil, NO);
        }
    }];
}
PHPzhong

PHObjectPlaceholder를 통해 PHAsset을 얻는 작업 추가

PHFetchResult *assets = [PHAsset fetchAssetsWithLocalIdentifiers:@[placeholderAsset.localIdentifier] 옵션:nil];
CompletionBlock(assets.firstObject, nil);
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿