Jaw Ali
Jaw Ali

Reputation: 205

UIImageView does not load image from URL iphone

I have imageView i want to load image from url but it is not loading i am using following code

               pdfView.hidden=NO;
       webView.hidden=NO;
       pictureImageView.hidden=NO;
      pdfView.backgroundColor=[UIColor clearColor];
      doneButton = [[UIBarButtonItem alloc]initWithTitle:@" Done " style:UIBarButtonItemStyleBordered target:self action:@selector(doneButtonClick)];            
      self.navigationItem.leftBarButtonItem =doneButton;
      webView.backgroundColor=[UIColor clearColor];


        NSLog(@"File Name is %@",fileName);

        NSLog(@"Enterning in the ImageSection");

        NSString *ImageURL = fileName;
        NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];
            pictureImageView.image = [UIImage imageWithData:imageData];         

Instead of this if i want to give from local like below then also it does not show image in imageView any idea how to load image from url thank

        pictureImageView.image=[UIImage imageNamed:@"starblue.png"];

Upvotes: 1

Views: 446

Answers (4)

Darshan Kunjadiya
Darshan Kunjadiya

Reputation: 3329

Use this code for show image in Url .

  NSString *image=[NSString stringWithFormat:@"Your URL"] stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
  NSLog(@"%@",image);
  NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:image]];
 Yourimageview=[UIImage imageWithData:imageData];

Upvotes: 1

Lithu T.V
Lithu T.V

Reputation: 20021

Code looks fine

Things to check

  1. Url contain a valid image
  2. Set ImageView alloc it properly set frame and then add image

UIImageView *pictureImageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 20, 50, 50)]; Then do the rest

This is synchronous image loading that will block the main thread activities.Prefer to use asynchronous image download using AFNetworking or AsynchImageview

Upvotes: 1

Bhavesh Nayi
Bhavesh Nayi

Reputation: 3656

 pictureImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgurl]]];

Upvotes: 0

Paras Joshi
Paras Joshi

Reputation: 20541

First set frame of that image and then write this line after add image in pictureImageView

            [self.view bringSubviewToFront:pictureImageView];

Upvotes: 1

Related Questions