Search code examples
iosobjective-cuiimageviewuiimageuikit

UIImageView from NSURL is Blank


I am not receiving any errors for attempting to display the image from a URL, however nothing displays. Do I have to download the image locally before attempting to display it? Excuse my ignorance, I'm a total iOS dev newbie haha.

- (void) viewDidLoad {
        NSData *dataBuff = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:@"https://i1.wp.com/www.penelopeperu.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-22-at-7.24.16-AM-e1503412089169.png"]];
        UIImage * imgBuffer = [[UIImage alloc] initWithData:dataBuff];
        img = [[UIImageView alloc] initWithImage:imgBuffer];
        img.image = randImgBuffer;
    }
}

(img is defined in my header file as a UIImageView)


Solution

  • Issue in code try this solution

    - (void) viewDidLoad {
            NSData *dataBuff = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:@"https://i1.wp.com/www.penelopeperu.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-22-at-7.24.16-AM-e1503412089169.png"]];
            UIImage * imgBuffer = [[UIImage alloc] initWithData:dataBuff];
            img.image = imgBuffer;
        }
    }