UIImageView - URL Image

Discussion in 'iOS Development' started by ipodmail09, May 30, 2009.

  1. ipodmail09

    ipodmail09 Member

    Joined:
    Mar 13, 2009
    Messages:
    269
    Likes Received:
    0
    Device:
    2G iPod touch
    G'day,


    Would you be able to show me or point me towards the code needed in Xcode for the UImageview to display a url image?

    Regards,
    Adam.
  2. Just_For_Now

    Just_For_Now Active Member

    Joined:
    Mar 21, 2009
    Messages:
    1,898
    Likes Received:
    1
    Device:
    4G iPod touch
    you can just make a webview and and place the url of the image there.
  3. ipodmail09

    ipodmail09 Member

    Joined:
    Mar 13, 2009
    Messages:
    269
    Likes Received:
    0
    Device:
    2G iPod touch
    yes but the only thing is i want the image to be centered.
  4. Just_For_Now

    Just_For_Now Active Member

    Joined:
    Mar 21, 2009
    Messages:
    1,898
    Likes Received:
    1
    Device:
    4G iPod touch
    cant you save the image and just place it on the view?
  5. ipodmail09

    ipodmail09 Member

    Joined:
    Mar 13, 2009
    Messages:
    269
    Likes Received:
    0
    Device:
    2G iPod touch
    no, it needs to be refreshed: live camera
  6. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    So the only thing I can think of right now is

    Code:
    [yourImageView setImage:[NSUrl urlWithString:[NSString stringWithFormat:@"path to your online image"]]];
    
    Again I'm not sure if this will work but give it a shot.
  7. ipodmail09

    ipodmail09 Member

    Joined:
    Mar 13, 2009
    Messages:
    269
    Likes Received:
    0
    Device:
    2G iPod touch
  8. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    yeah I got it completely wrong
    Code:
    imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: @"path/to/your/image"]]];
    
  9. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    That is Sync that would block the main thread for a few secs or longer.
  10. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    Of course that would block the main thread, so spawn a new one, or use NSOperation!


    Code:
    @interface ImageDownloadOperation : NSOperation {
            SEL _action;
            id _target;
            NSURL *_imgURL;
    }
    
    - (id)initWithImageURL:(NSString *)url target:(id)target action:(SEL)action;
    - (void)main;
    - (void)dealloc;
    
    @end
    
    @implementation ImageDownloadOperation
    
    - (id)initWithImageURL:(NSString *)url target:(id)target action:(SEL)action {
            if(self = [super init]) {
                    _action = action;
                    _target = target
                    if(url == nil) {
                            return nil;
                    } else {
                            _imgURL = [[NSURL alloc] initWithString:[url copy]];
                    }
            }
    
            return self;
    }
    
    - (void)main {
            NSData *imgData = [[NSData alloc] initWithContentsOfURL:_imgURL];
            UIImage *image = [[UIImage alloc] initWithData:imgData];
    
            if([_target respondsToSelector:_action])
                    [_target performSelectorOnMainThread:_action withObject:image waitUntilDone:NO];
    
            [imgData release];
            [image release];
    }
    
    - (void)dealloc {
            [imgURL release];
    
            [super dealloc];
    }
    Then call that somewhere in your code.

    Code:
    ImageDownloadOperation *operation = [[ImageDownloadOperation alloc] initWithURL:myURLString target:self action:@selector(didGrabImage:)];
    [operation start];
    [operation release];
    Then when you get the notification:

    Code:
    - (void)didGrabImage:(UIImage *)image {
            myImageView.image = image;
    }
    This may not all be correct, but it's extremely hard to type these things out in this box without syntax highlighting or tabbing.

Share This Page