0

我正在尝试将 URL 从 传输UITableViewControllerUIViewController,但由于某种原因,图像没有UIImageView从提交的 URL 中显示。这是我的代码:

表视图.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *url = @"http://www.macdigger.ru/wp-content/uploads/2013/02/Apple-Nokia-Samsung-1.jpg";

    ImageViewController *imageView = [[ImageViewController alloc] init];        
    [self.navigationController pushViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"imageViewController"] animated:YES];

    [imageView setDetailItem:url];

}

ImageViewController.m

@interface ImageViewController ()
- (void)configureView;
@end

@implementation ImageViewController

- (void)setDetailItem:(id)newUrl
{
    NSLog(@"%@", newUrl);
    self.urlOfImage = newUrl;
    [self configureView];

}

- (void)configureView
{
        NSURL *url = [NSURL URLWithString:_urlOfImage];
        NSLog(@"%@",url); //There URL is normally displayed in the log
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:data];
        _imageView.image = image;//And then the picture does not want to output
}

在日志中显示传递的 URL(粗体)。事实证明,URL 本身被传递,但是UIImageView,不显示来自该 URL 的图像。

PPS数据以以下形式显示在日志中"<ffd8ffe1 00184578 69660000 49492a00 08000000 00000000 00000000 ffec0011 4475636b 79000100 04000000 460000ff e1031b68 7474703a 2f2f6e73 2e61646f 62652e63 6f6d2f78 61702f31 2e302f00 3c3f7870 61636b65 74206265 67696e3d 22efbbbf 22206964...>"

4

2 回答 2

1

可能是因为 ViewController 尚未加载而出错,尝试检查它是否已在setDetailItem方法中加载,如果未加载 - viewDidLoad 中的 configureView:

- (void)setDetailItem:(id)newUrl
{
    NSLog(@"%@", newUrl);
    self.urlOfImage = newUrl;
    if ([self isViewLoaded]) {
        [self configureView];
    }

}

- (void)viewDidLoad 
{
    [super viewDidLoad];
    if (self.urlOfImage) {
        [self configureView]
    }
}
于 2013-02-18T15:01:57.447 回答
1

你为什么不做另一个初始化器?

ImageViewController *imageView = [[ImageViewController alloc] initWithURL:urlToGo];

在 .m 文件中,在顶部:

@interface ImageViewController ()
@property (nonatomic, strong) NSURL *url;
@end

- (id)initWithURL:(NSURL *url){
   self = /* any kind of normal initialization, xib or storyboard */ [super init];
   if (self) {
       _url = url;
   }
}

- (void)viewDidLoad {
   [super viewDidLoad];
   [self configureView];
}
于 2013-02-18T16:16:44.810 回答