我正在尝试使用 imagePickerControl 选择图像并使用提供的 URL,将其打开并将其显示在 imageview 上。如果我使用 UIImagePickerControllerOriginalImage 来设置我的图像,它工作正常,但我不想将图像存储在我的数据库中,我只想存储 URL。在我将它进一步扩展到我的代码之前,我想确保它能够工作。Bellow 是从 PickerController 返回所选图像并尝试显示图像的代码。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
self.imageURL=[info objectForKey:UIImagePickerControllerReferenceURL];
CCLog(@"Image =%@",[info objectForKey:UIImagePickerControllerReferenceURL]);
// self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
switch(status){
case ALAuthorizationStatusDenied: {
CCLog(@"not authorized");
break;
}
case ALAuthorizationStatusRestricted: {
CCLog(@"Restricted");
break;
}
case ALAuthorizationStatusNotDetermined: {
CCLog(@"Undetermined");
break;
}
case ALAuthorizationStatusAuthorized: {
CCLog(@"Authorized");
CCLog(@"self.imageURL=%@",self.imageURL);
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
__block UIImage *returnValue = nil;
[library assetForURL:self.imageURL resultBlock:^(ALAsset *asset) {
returnValue = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
} failureBlock:^(NSError *error) {
NSLog(@"error : %@", error);
}];
[self.imageDisplay setImage:returnValue];
[self.imageDisplay setNeedsDisplay];
break;
}
default: {
CCLog(@"Unknown hit default");
break;
}
}
// You have the image. You can use this to present the image in the next view like you require in `#3`.
}
当它运行时,当我跟踪它并且没有显示图像时,returnValue 会出现为零。我从https://stackoverflow.com/a/13276649/3723298获得了代码
谢谢