从NSURL UIImagepicker Controller iOS 9

时间:2016-10-18 07:02:39

标签: ios

我从UIImagePickerController中选择了图像,之后我获得了其委托方法的信息字典。我想从NSURL获取图像名称请为iOS 9建议代码,因为不推荐使用ALAsset库代码来获取图像名称。

NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL]; 

1 个答案:

答案 0 :(得分:0)

导入资产库

试试这个,您将在cstmGetUploadImageName方法中获取图像名称

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    //    selected image
    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.img_profilepic.image = chosenImage;

    //    NSString *setimageName;
    // get the ref url
    NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];

    // define the block to call when we get the asset based on the url (below)
    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset)
    {
        ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation];
        NSLog(@"[imageRep filename] : %@", [imageRep filename]);
        [self cstmGetUploadImageName:[imageRep filename]];

    };

    // get the asset library and fetch the asset based on the ref url (pass in block above)
    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
    [assetslibrary assetForURL:refURL resultBlock:resultblock failureBlock:nil];


    //    upload image method
    //[self cstmUploadImage:chosenImage withName:@"sdf"];
        [picker dismissViewControllerAnimated:YES completion:NULL];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    [picker dismissViewControllerAnimated:YES completion:NULL];

}


-(void)cstmGetUploadImageName:(NSString *)filename{
    NSString  *tempFileName = filename;
    NSLog(@"%@",tempFileName);
}