ios - UIImageView 不显示来自 UITableViewController 的设置图像

标签 ios uiimageview imageview uitableview

我有一个 UITableViewController带有图像名称列表。单击名称时,图像的 URL 设置在 ImageViewController 中。然后是 UITableViewController转至 ImageViewController包含 UIImageView (与 socket 相连)。在 ImageViewControllerviewDidLoad我设置了UIImageView从 URL 到图像,但是当我在模拟器上运行它时,只有一个黑屏出现在图像应该出现的位置。

对转场的一点视觉描述(其中 -> 表示转场):

  • ImageListTableViewController –> ImageViewController(有 UIImageView )
  • ImageListTableViewController.m
    // The delegate here is the ImageViewController
    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([segue.identifier isEqualToString:@"Show Image"]) {
            [self setDelegate:segue.destinationViewController];
        }
    }
    // getting URL and calling the protocol method (in ImageViewController) which sets
    // the image as an @property
    #pragma mark - Table view delegate
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSDictionary *imageDict = [self.imageArray objectAtIndex:indexPath.row];
        NSURL *imageURL = [FlickrFetcher urlForPhoto:imageDict format:FlickrPhotoFormatLarge];
        NSLog([imageURL description]);
        [self.delegate imageListTableViewController:self withURL:imageURL];
    }
    
    ImageViewController.m
    #import "ImageViewController.h"
    
    @implementation ImageViewController
    
    @synthesize image = _image;
    @synthesize imageView = _imageView;
    
    - (void)imageListTableViewController:(ImageListTableViewController *)sender 
                                 withURL:(NSURL *)imageURL;
    {
        NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
        self.image = [UIImage imageWithData:imageData];
    }
    
    #pragma mark - View lifecycle
    
    
    // Implement loadView to create a view hierarchy programmatically, without using a nib.
    - (void)loadView
    {
      // nothing
    }
    
    
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self.imageView setImage:self.image];
    }
    
    
    - (void)viewDidUnload
    {
        [self setImageView:nil];
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    
    @end
    

    关于发生了什么的任何想法?
    谢谢!

    最佳答案

    不是真正使用委托(delegate)的正确方法。试试这个:
    ImageViewController.h :

    @property(nonatomic,strong) NSURL *imageURL;
    

    ImageViewController.m
    @synthesize imageURL;
    

    删除 imageListTableViewController

    更改viewDidLoad :
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
        self.image = [UIImage imageWithData:imageData];
    }
    

    添加到 viewDidUnload :
    [self setImageURL:nil];
    

    然后在你的ImageListTableViewController.m :
    更改prepareForSegue :
    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([segue.identifier isEqualToString:@"Show Image"]) {
            NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
            NSDictionary *imageDict = [self.imageArray objectAtIndex:indexPath.row];
            NSURL *imageURL = [FlickrFetcher urlForPhoto:imageDict format:FlickrPhotoFormatLarge];
    
            // Set the property on our Dest VC
            ImageViewController *ivc = segue.destinationViewController;
            ivc.imageURL = imageURL;
    
        }
    }
    

    更改您的 didSelectRowAtIndexPath :
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [self performSegueWithIdentifier:@"Show Image" sender:nil];
    }
    

    确保您已将该 Segue 连接到 VC 本身而不是 TableView。如果你不这样做,Segue 会在 didSelectRowAtIndexPath 之前被解雇。被解雇。在这种情况下,它不会产生影响,但对于真正的控制 - 养成连接到 VC 并在需要时调用 performSegue 的习惯。

    关于ios - UIImageView 不显示来自 UITableViewController 的设置图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9626802/

    相关文章:

    ios - Realm 查询属性(property)总和

    ios - 如何使用这个宏

    iphone - iOS 6 : background imageview in iPhone 5

    android - 如何在java中获取图像名称?

    android - Monodroid - ImageView -> SetImageURI 崩溃应用程序

    ios - 没有找到合适的申请记录。验证应用程序时 - 初学者

    ios - 如何在 SwiftUI 的 TabView 中添加底部曲线?

    ios - 如何一次只允许一个UIImageView被触摸

    iphone - UIImageView 带有大图像。问题

    android - 在主屏幕小部件上使用 Canvas 绘制