从保存的相机胶卷参考 url 设置 uiimageview
Posted
技术标签:
【中文标题】从保存的相机胶卷参考 url 设置 uiimageview【英文标题】:set uiimageview from saved camera roll reference url 【发布时间】:2014-04-20 15:26:17 【问题描述】:在资产库如何与参考 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。怎么没设置?
【问题讨论】:
或许这篇文章可以帮到你:wooptoot.com/loading-images-from-the-ios-photo-library 已经看到了,不完全是我想要做的。这只是处理最初选择图像。我想要做的是在最初选择它并使用引用 url 设置它之后保存引用 【参考方案1】:如何让用户从图库中选择图片?
假设你有一个控制器 MyDummyController,它显示了一个“Pick An Image”按钮。点击该按钮时,您想打开手机图库。首先确保你的控制器实现了 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"];
如何从图片 URL 中提取字符串引用?
在不同的持久化系统中,例如 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/
【讨论】:
以上是关于从保存的相机胶卷参考 url 设置 uiimageview的主要内容,如果未能解决你的问题,请参考以下文章