Reputation: 2930
I have a UIImageView
and I cannot get the image and set for it to show up.
in viewDidLoad
albumContainer = [[UIImageView alloc]initWithFrame:CGRectMake(112, 7, 97, 97)];
[self.view addSubview:albumContainer];
Method being called from different class
NSLog(@"URL:%@",url);
//URL is defined by different class
//URL is http://www.....882546328.jpg (I abbreviated it, but it is a valid url)
NSData *imageData = [NSData dataWithContentsOfURL:url];
NSLog(@"URL LENGTH:%d",[imageData length]);
// URL LENGTH:90158
UIImage *tempImage = [UIImage imageWithData:imageData];
[albumContainer setImage:tempImage];
FULL CODE
PlayerViewController
-(void)viewDidLoad{
[super viewDidLoad];
playerView = [[AlbumViewController alloc]initWithNibName:@"AlbumViewController" bundle:nil];
}
-(void)playMusic{
playerView.url = [NSURL URLWithString:imagePathForm];
playerView.songData = receivedData;
[loadingSong stopAnimating];
[playerView play];
}
AlbumViewController
- (void)viewDidLoad
{
[super viewDidLoad];
albumContainer = [[UIImageView alloc]initWithFrame:CGRectMake(112, 7, 97, 97)];
[self.view addSubview:albumContainer];
url = [NSURL URLWithString:@""];
}
-(void)play{
NSLog(@"SONGDATA:%d",[songData length]);
player = [[AVAudioPlayer alloc] initWithData:songData error: nil];
[player stop];
player.delegate = self;
player.numberOfLoops = 0;
player.volume = 0.5;
[player prepareToPlay];
[player play];
NSLog(@"URL:%@",url);
NSData *imageData = [NSData dataWithContentsOfURL:url];
NSLog(@"URL LENGTH:%d",[imageData length]);
UIImage *tempImage = [UIImage imageWithData:imageData];
[albumContainer setImage:tempImage];
}
I checked and albumContainer logged as NULL
Upvotes: 1
Views: 1599
Reputation: 40995
Have you checked that the controller's view has been loaded before you set the image? UIViewControllers don't load their view until it's displayed, and they sometimes unload it when offscreen, so viewDidLoad may not have been called at the point when you call your
[albumContainer setImage:tempImage];
From outside the controller. Try logging albumContainer to see if it's nil before you set the image, like this:
NSLog(@"albumContainer: %@", albumContainer); //might log as null
[albumContainer setImage:tempImage];
To force the controller's view to load, you can say:
[controller view]; // this will load the view and call viewDidLoad
[albumContainer setImage:tempImage];
But you may be better off creating your albumContainer in the initWithNibName:bundle: method of your view controller instead of in the viewDidLoad, like this:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)bundleOrNil
{
if ((self = [super initWithNibName:nibNameOrNil bundle:bundleOrNil]))
{
albumContainer = [[UIImageView alloc]initWithFrame:CGRectMake(112, 7, 97, 97)];
[self.view addSubview:albumContainer];
}
return self;
}
That way it will be created at the same time as the controller even if the view hasn't loaded yet.
Upvotes: 1