这是我的应用程序的简化版本,它从iPhone / iPad图像库中获取图像URL,将该URL的字符串存储在核心数据中,然后将其显示在另一个控制器中。我认为正确的URl存储在核心数据中但在以后检索时不会显示。
为了尝试排除故障,我有一个应用程序可以从iPhone / iPad图像库中获取图片并将其显示在屏幕上。然后按下按钮以切换到第二个视图控制器并再次显示。它不会出现在第二个视图控制器上。我在第一个场景中显示它与我在第二个控制器中使用的方法相同,只是为了表明该方法可以提供正确的URL。我将URL作为控制器之间的字符串传递,以模拟存储在核心数据中的URL,因此将其作为NSURL传递并不是此示例的选项。
首先查看controller.h和.m文件
//
// patViewController.h
// pictureAssetTest
//
// Created by Chip Cox on 8/10/14.
// Copyright (c) 2014 Home. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "CCExtras.h"
#import <AssetsLibrary/AssetsLibrary.h>
@interface patViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@end
//
// patViewController.m
// pictureAssetTest
//
// Created by Chip Cox on 8/10/14.
// Copyright (c) 2014 Home. All rights reserved.
//
#import "patViewController.h"
#import "patSecondVC.h"
@interface patViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageControl;
@property (nonatomic,strong) NSURL *imageURL;
@end
@implementation patViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)selectPictureFromLibrary:(UIButton *)sender
{
[self startPhotoLibraryFromViewController:self usingDelegate:self];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
self.imageURL=[info objectForKey:UIImagePickerControllerReferenceURL];
CCLog(@"Image =%@",[info objectForKey:UIImagePickerControllerReferenceURL]);
[self displayImageFromURL:self.imageURL];
}
- (void) displayImageFromURL:(NSURL*)urlIn
{
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(@"urlIn=%@",urlIn.pathComponents);
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
__block UIImage *returnValue = nil;
[library assetForURL:urlIn resultBlock:^(ALAsset *asset) {
returnValue = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
dispatch_async(dispatch_get_main_queue(), ^{
[self.imageControl setImage:returnValue];
[self.imageControl setNeedsDisplay];
});
} failureBlock:^(NSError *error) {
NSLog(@"error : %@", error);
}];
break;
}
default: {
CCLog(@"Unknown hit default");
break;
}
}
}
- (BOOL) startPhotoLibraryFromViewController: (UIViewController*) controller
usingDelegate: (id <UIImagePickerControllerDelegate,
UINavigationControllerDelegate>) delegate {
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// Displays a control that allows the user to choose picture or
// movie capture, if both are available:
cameraUI.mediaTypes =
[UIImagePickerController availableMediaTypesForSourceType:
UIImagePickerControllerSourceTypePhotoLibrary];
// Hides the controls for moving & scaling pictures, or for
// trimming movies. To instead show the controls, use YES.
cameraUI.allowsEditing = NO;
cameraUI.delegate = delegate;
[controller presentViewController:cameraUI animated:YES completion:nil];
return YES;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.destinationViewController isKindOfClass:[patSecondVC class]]) {
CCLog(@"Preparing for segue to %@",segue.destinationViewController);
patSecondVC *dvc=segue.destinationViewController;
dvc.transferURL=self.imageURL.absoluteString;
} else {
CCLog(@"Preparing to segue to unknown destination view controller %@",segue.destinationViewController);
}
}
@end
以下代码是第二个视图控制器.h和.m文件。
//
// patSecondVC.h
// pictureAssetTest
//
// Created by Chip Cox on 8/10/14.
// Copyright (c) 2014 Home. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import "CCExtras.h"
@interface patSecondVC : UIViewController
@property (nonatomic,strong) NSString *transferURL;
@end
//
// patSecondVC.m
// pictureAssetTest
//
// Created by Chip Cox on 8/10/14.
// Copyright (c) 2014 Home. All rights reserved.
//
#import "patSecondVC.h"
@interface patSecondVC ()
@property (weak, nonatomic) IBOutlet UIImageView *imageControl;
@end
@implementation patSecondVC
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
CCLog(@"photo url=%@",self.transferURL);
[self displayImageFromURL:[NSURL fileURLWithPath:self.transferURL]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) displayImageFromURL:(NSURL*)urlIn
{
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(@"urlIn=%@",urlIn.pathComponents);
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
__block UIImage *returnValue = nil;
[library assetForURL:urlIn resultBlock:^(ALAsset *asset) {
returnValue = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
dispatch_async(dispatch_get_main_queue(), ^{
[self.imageControl setImage:returnValue];
[self.imageControl setNeedsDisplay];
});
} failureBlock:^(NSError *error) {
NSLog(@"error : %@", error);
}];
break;
}
default: {
CCLog(@"Unknown hit default");
break;
}
}
}
@end
由于
添加了日志输出
2014-08-11 17:05:37.194 pictureAssetTest[2544:60b] <-[patViewController imagePickerController:didFinishPickingMediaWithInfo:] - 45> Image =assets-library://asset/asset.PNG?id=5E208B2D-65CC-4F46-8F22-DD2E8006AF1B&ext=PNG
2014-08-11 17:05:37.195 pictureAssetTest[2544:60b] <-[patViewController displayImageFromURL:] - 68> Authorized
2014-08-11 17:05:37.196 pictureAssetTest[2544:60b] <-[patViewController displayImageFromURL:] - 69> urlIn=(
"/",
"asset.PNG"
)
2014-08-11 17:05:39.149 pictureAssetTest[2544:60b] <-[patViewController prepareForSegue:sender:] - 116> Preparing for segue to <patSecondVC: 0xa49c610>
2014-08-11 17:05:39.150 pictureAssetTest[2544:60b] <-[patSecondVC viewDidLoad] - 31> photo url=assets-library://asset/asset.PNG?id=5E208B2D-65CC-4F46-8F22-DD2E8006AF1B&ext=PNG
2014-08-11 17:05:39.151 pictureAssetTest[2544:60b] <-[patSecondVC displayImageFromURL:] - 59> Authorized
2014-08-11 17:05:39.151 pictureAssetTest[2544:60b] <-[patSecondVC displayImageFromURL:] - 60> urlIn=(
"/",
"assets-library:",
asset,
"asset.PNG?id=5E208B2D-65CC-4F46-8F22-DD2E8006AF1B&ext=PNG"
)
答案 0 :(得分:1)
在第二个vc中将调用更改为
[self displayImageFromURL:[NSURL URLWithString:self.transferURL]];
那应该解决它!