uiimageview 无法使用 URLWithString 工作:

uiimageview is not Working using URLWithString:

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

我想在代码中使用 URL link 显示图像, 但是没有任何结果。

问题出在 url 中的空格符号。

您可以使用:

stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]

完整代码:

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]]];