从保存的相机胶卷参考URL设置uiimageview

时间:2014-04-20 15:26:18

标签: ios objective-c uiimageview alassetslibrary

对于资源库如何与引用URL一起工作,我可能还有一些东西尚未理解,但我现在正在nsdefaults中保存引用。我想在屏幕加载时显示参考图像,但它不起作用。这是我得到图像的方式:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
  [[NSUserDefaults standardUserDefaults]setObject:[info objectForKey:UIImagePickerControllerReferenceURL] forKey:@"profilePic"];
  [pics addObject:[info objectForKey:UIImagePickerControllerReferenceURL]];
  [library assetForURL:[pics objectAtIndex:pics.count-1] resultBlock:^(ALAsset *asset){
      UIImage *copyofOriginal = [UIImage imageWithCGImage:[[asset defaultRepresentation]fullScreenImage] scale:0.5 orientation:UIImageOrientationUp];
      profilePic.image = copyofOriginal;
  }failureBlock:nil];
}

现在,当应用程序再次启动时,我正试图像这样回来,但它给了我一个空白的图像:

if (![[[NSUserDefaults standardUserDefaults]valueForKey:@"profilePic"] isEqualToString:@""]) {
    [library assetForURL:[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]valueForKey:@"profilePic"]] resultBlock:^(ALAsset *asset){
        UIImage *copyofOriginal = [UIImage imageWithCGImage:[[asset defaultRepresentation]fullScreenImage] scale:0.5 orientation:UIImageOrientationUp];
        profilePic.image = copyofOriginal;
    }failureBlock:nil];
}

记录[info objectForKey:UIImage ...]并从NSUserDefaults保存的值给出了完全相同的参考URL。怎么没有设置?

1 个答案:

答案 0 :(得分:1)

如何让用户从图库中选择图像?

想象一下,你有一个控制器MyDummyController,它显示一个按钮“选择图像”。点击该按钮时,您想打开手机图库。首先确保你的控制器实现了UIImagePickerControllerDelegate。

@interface MyDummyController : UIViewController<UIImagePickerControllerDelegate>

接下来,在实现文件中实现以下方法。

    // make sure this method is invoked when you tap "Pick An Image" button
-(void)onTapPickAnImage
{
   UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
   imagePicker.delegate = self;
   imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
   [self presentViewController:imagePicker animated:YES completion:nil];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
       // here is the image user selected
       UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
       // here is the URL to the image
       NSURL *url = [info objectForKey:@"UIImagePickerControllerReferenceURL"];
    }

如何从图片网址中提取字符串参考?

在不同的持久性系统中,例如Core Data,您无法直接保存NSURL。这意味着,如果您想在某处存储所选图像的引用,则需要在字符串中使用它。它可以像下面这样完成:

NSURL *url = [info objectForKey:@"UIImagePickerControllerReferenceURL"];
NSString *ref = url.absoluteString;
// now you can persist your string reference somewhere

如何使用字符串引用再次加载图像?

// here we are loading the string reference to image we want to load
NSString *ref = [self loadMyImageRef]; // loadMyImageRef is a dummy method, you can put your implementation here

// ALAssetsLibrary provides a way to access photos/videos in gallery programmatically
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:[NSURL URLWithString:ref] resultBlock:^(ALAsset *asset) {
   // here we have received the image data, now you can easily display it wherever you like
   UIImage *image = [UIImage imageWithCGImage:asset.defaultRepresentation.fullResolutionImage];
   NSLog(@"Image loaded successfully!");
} failureBlock:^(NSError *error) {
   NSLog(@"An error occurred while loading image: %@", error.description);
}];

<强>来源:

http://www.to-string.com/2014/04/30/how-to-extract-string-reference-to-an-image-in-ios-and-then-load-the-image-later/