crazy2431
crazy2431

Reputation: 801

How to display a image downloaded from URL in iPhone?

I am downloading a image from the url and saving it. When i try to display the same image in UI using ImageView I am not able to do it. So please suggest what changes I need to be done in the following code.

Thank you.

{

NSLog(@"Downloading...");

    UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.objectgraph.com/images/og_logo.png"]]];

    NSLog(@"%f,%f",image.size.width,image.size.height);

    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSLog(@"%@",docDir);

    NSLog(@"saving png::::%@", docDir);
    NSString *pngFilePath = [NSString stringWithFormat:@"%@/test.png",docDir];
    NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
    [data1 writeToFile:pngFilePath atomically:YES];

 NSString *sss = [docDir stringByAppendingString:@"/test.png"];

    imageView1.image = [UIImage imageNamed:sss];

    NSLog(@"------%@",sss);

[image release];

}

Upvotes: 0

Views: 1828

Answers (2)

d.ennis
d.ennis

Reputation: 3455

How is your imageView initalized? Make sure to also specify the imageView.frame.

e.G.:

CGRect frame = CGRectMake(xPositionInView,yPositionInView,
                          image.size.width,image.size.height); 
imageView.frame = frame;

Upvotes: 0

ader
ader

Reputation: 5393

UIImage *theUIimage = [UIImage imageNamed:sss];
image = [[UIImageView alloc] initWithImage:theUIimage];
[imageView1 addSubview:image];

Upvotes: 2

Related Questions