Search code examples
iosobjective-cuiimageviewuiimagensdata

uiimageview is not Working using URLWithString:


_profileImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@".../images/Profile/Pratik Maniya Photo.jpg"]]];

I want to display image using the URL link in the code, but there is not any result.


Solution

  • The problem is in the spaces symbols in url.

    You can use:

    stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]
    

    Full code:

    NSString *base_url = @"https://safesocpgm.000webhostapp.com/images/Profile/Pratik Maniya Photo.jpg";
    NSString* encoded_url = [base_url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    _profileImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:encoded_url]]];