-2

I need to load a image from a URL into a UIImageView. Each time the URL would be different because i am JSON parsing a website. I am able to get the URL. However when i run the app, there is no image in the UIImageView.

This is my code

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if ([eventNameDesc containsString:@"src="]) {
        eventNameDesc = @"";
    }
    else {
        eventDescription.text = eventNameDesc;
    }
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(shareButtonPressed)];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)shareButtonPressed {
    NSString *shareText = eventNameDesc;
    NSArray *itemsToShare = @[shareText];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
    activityVC.excludedActivityTypes = @[];
    [self presentViewController:activityVC animated:YES completion:nil];
}

- (void)loadImage {
    NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:self.imageURL]];
    UIImage* image = [[UIImage alloc] initWithData:imageData];
    [self performSelectorOnMainThread:@selector(displayImage:) withObject:image waitUntilDone:NO];
}

- (void)displayImage:(UIImage *)image {
    [self.ImageView setImage:image]; //UIImageView
}

@end

What is wrong with my code? Somebody please help.

Dinesh Sekar
  • 91
  • 1
  • 1
  • 8

1 Answers1

0

You can make a UIImage object using the code provided here

After that you can add your UIImage object to any UIImageView object. This is the code that works for me.

UIImage *anImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://4.bp.blogspot.com/-K7vl-ShXrNc/VQU-D0NTFgI/AAAAAAAAAOg/aBIUOwF2nEQ/s1600/contact.png"]]];
UIImageView *temp = [[UIImageView alloc] initWithImage:anImage];
[self.view addSubview:temp];

Here is a screenshot.

Community
  • 1
  • 1
Shamas S
  • 7,507
  • 10
  • 46
  • 58
  • @DineshSekar Please see the code that I am using on top and see the screenshot where its working for me. – Shamas S Mar 18 '15 at 14:10
  • i copied your code and pasted it into my `viewDidLoad`. But it didn't work for me. – Dinesh Sekar Mar 18 '15 at 14:23
  • It works for me if i do it in a new project. However when i do it in my original project it doesn't work – Dinesh Sekar Mar 18 '15 at 14:28
  • @DineshSekar where does your self.Imageview come from? Is it initialized? – Shamas S Mar 18 '15 at 14:38
  • i think that the `UIImageView` that your code creates is being blocked by my navigation controller. Is to possible for me to place it somewhere else programmatically. – Dinesh Sekar Mar 18 '15 at 14:45
  • it comes from my .h file – Dinesh Sekar Mar 18 '15 at 14:48
  • @DineshSekar in your displayImage function create new UIImageView and init it with the UIImage object passed. and add it. That worked for me. // UIImageView *asdf = [[UIImageView alloc] initWithImage:image]; // [self.view addSubview:asdf]; – Shamas S Mar 18 '15 at 14:49
  • @DineshSekar can you show me the code where you are creating new uiimageView. – Shamas S Mar 18 '15 at 14:55
  • @DineshSekar start with creating your NSData, UIImage and UIImageView in your viewDidLoad. See if it works there. Then shift the entire code on to the selector and onwards and figure out where it stops working. – Shamas S Mar 18 '15 at 14:58
  • `- (void)displayImage:(UIImage *)image { UIImage *anImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://4.bp.blogspot.com/-K7vl-ShXrNc/VQU-D0NTFgI/AAAAAAAAAOg/aBIUOwF2nEQ/s1600/contact.png"]]]; UIImageView *temp = [[UIImageView alloc] initWithImage:anImage]; [self.view addSubview:temp]; }` This is my code – Dinesh Sekar Mar 18 '15 at 14:59
  • i don't think that it stops working. I think that it cannot be seen because when i try it without the navigation bar it works. – Dinesh Sekar Mar 18 '15 at 15:01
  • Awesome.. I am so glad :D How did it work by the way? – Shamas S Mar 18 '15 at 15:09
  • I created my own `UIImageView` and then added this code into the `viewDidLoad` `UIImage *anImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.imageURL]]]; imageView.image = anImage;` – Dinesh Sekar Mar 18 '15 at 15:11