簡體   English   中英

UIImageView不顯示來自UITableViewController的設置圖像

[英]UIImageView not displaying set image from UITableViewController

我有一個帶有圖像名稱列表的UITableViewController 單擊某個名稱時,將在ImageViewController設置圖像的URL,然后UITableViewController轉換為包含UIImageView (與插座連接)的ImageViewController ImageViewControllerviewDidLoad我將UIImageView設置為URL中的圖像,但是當我在模擬器上運行它時,圖像應該只出現黑屏。

segue的一點視覺描述(其中 - >表示segue):

  • 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

關於發生了什么的任何想法? 謝謝!

不是使用委托的正確方法。 試試這個:在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。 如果不這樣,那么Sese會在觸發didSelectRowAtIndexPath之前被解雇。 在這種情況下,它不會有所作為,但對於真正的控制 - 養成連接VC並在需要時調用performSegue的習慣。

嘗試調用此方法:

[self.imageView setImage:self.image];

在里面 -

(void)imageListTableViewController:(ImageListTableViewController *)sender 
                             withURL:(NSURL *)imageURL;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM