UIImage

Discussion in 'iOS Programming' started by forrestgrant, Aug 26, 2008.

  1. macrumors member

    Joined:
    Jun 24, 2008
    #1
    Hi there,

    I load an image like so:
    Code:
    UIImage *image = [UIImage imageNamed:@"myimage.png"];
    
    But what if I want to load an image from the internet? Something to the effect of:
    Code:
    UIImage *image = [UIImage imageNamed:@"http://myurl.com/myimage.png"];
    
    Any help is appreciated. Thanks!
     
  2. macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #2
    try
    [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:mad:"http://myurl.com/myimage.png"];
     
  3. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    This will work (although you need two more ] before the ;) but will block the thread until the data is retrieved over the network. If you do this at startup of your application this may lead to a very long wait until your application is ready for use which your users are not going to like.

    If this is the case you can use a NSURLConnection to dowload the data asynchronously and then create the image once the data is downloaded. This is obviously much more complicated...
     

Share This Page