如何在 iOS 中将 URL 链接转换为 UIImage?

Rah*_*rma 1 objective-c uiimage ios

我正在使用 UIImage View 并像下面的代码一样添加图像

 NSURL *imageURL = [NSURL URLWithString:@"http://d1mxp0yvealdwc.cloudfront.net/e92c939d-e83b-4592-b367-327fa67339fb/1001 123.jpg"];
NSLog(@"imge %@",imageURL);

NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
    NSLog(@"imge %@",imageData);
UIImage *image = [UIImage imageWithData:imageData];
   NSLog(@"imge %@",image);
[image_view setImage:image];
Run Code Online (Sandbox Code Playgroud)

但所有 nslog 都显示 null 实际上 url 存在问题,但此显示图像在网络上,所以请告诉我这里有什么问题。

小智 5

尝试以下代码:

NSURL *aURL = [NSURL URLWithString:[@"http://d1mxp0yvealdwc.cloudfront.net/e92c939d-e83b-4592-b367-327fa67339fb/1001%20123.jpg" stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];

UIImage *aImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:aURL]];
Run Code Online (Sandbox Code Playgroud)

这工作正常...!!!