PHImageFileURLKey对应的值没数据

2019-05-17  本文已影响0人  恩莱客
#pragma mark -imagePickerController delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:NO completion:nil];
    NSMutableDictionary *mediaInfo = [[NSMutableDictionary alloc] initWithDictionary:info];
    UIImage *tempImage ;
    tempImage = [mediaInfo objectForKey:UIImagePickerControllerOriginalImage];
    //保证图片方向正向
    UIImage *upImage = [CommUtls fixOrientation:tempImage];
    UIImage *compressImage = [[ImageCompression shareImageCompression] compessImage:upImage comWidth:640 comHeight:972];
    
    NSDictionary *imageInfo = nil;
    //获取本地路径
    if (info[@"PHImageFileURLKey"]) {
      imageInfo = [NSDictionary dictionaryWithObjectsAndKeys:compressImage,@"image", [NSNumber numberWithFloat:ratio],@"ratio", [info[@"PHImageFileURLKey"] absoluteString],  @"local_path", nil];
    }else{
      imageInfo = [NSDictionary dictionaryWithObjectsAndKeys:compressImage,@"image", [NSNumber numberWithFloat:ratio],@"ratio", nil];
}
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTI_TRANSTER_IMAGE object:imageInfo];
    }
}

获取本地图片功能,对应key为PHImageFileURLKey,注意通过PHImageFileURLKey会有空的情况,需要添加空判断。
Stack Overflow给出的解决方案:

PHImageRequestOptions * imageRequestOptions = [[PHImageRequestOptions alloc] init];
 [[PHImageManager defaultManager]
             requestImageDataForAsset:asset
                            options:imageRequestOptions
                      resultHandler:^(NSData *imageData, NSString *dataUTI,
                                      UIImageOrientation orientation, 
                                      NSDictionary *info) 
     {
          NSLog(@"info = %@", info);
          if ([info objectForKey:@"PHImageFileURLKey"]) {

               NSURL *path = [info objectForKey:@"PHImageFileURLKey"];
               // if you want to save image in document see this.
               [self saveimageindocument:imageData withimagename:[NSString stringWithFormat:@"DEMO"]];
          }                                            
    }];

-(void) saveimageindocument:(NSData*) imageData withimagename:(NSString*)imagename{
    NSString *writePath = [NSString stringWithFormat:@"%@/%@.png",[Utility getDocumentDirectory],imagename];
    if (![imageData writeToFile:writePath atomically:YES]) {
        // failure
        NSLog(@"image save failed to path %@", writePath);

    } else {
        // success.
        NSLog(@"image save Successfully to path %@", writePath);
    }

}
+ (NSString*)getDocumentDirectory {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [paths objectAtIndex:0];
}
上一篇下一篇

猜你喜欢

热点阅读