【发布时间】:2014-03-19 22:56:01
【问题描述】:
我试图从下载的文件中获取图像。首先,我获取图像并下载它们。
-(void)saveMoodImage{
for (NSDictionary *dc in self.arrMood) {
NSLog(@"dc: %@",dc);
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSString *urlString = [NSString stringWithFormat:@"%@/images/%@",kRootUrl,dc[@"image"]];
NSString* encodedUrl = [urlString stringByAddingPercentEscapesUsingEncoding:
NSUTF8StringEncoding];
NSURL *URL = [NSURL URLWithString:encodedUrl];
[[MUtility sharedObject].arrMoodImageUrl addObject:URL];
[[MUtility sharedObject].arrMoodName addObject:dc[@"text"]];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectoryPath = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]];
return [documentsDirectoryPath URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
NSLog(@"File downloaded to: %@", filePath);
[[MUtility sharedObject].arrMoodImagePath addObject:filePath];
}];
[downloadTask resume];
}
} 结果日志:
2014-03-20 00:30:48.174 ImageTutorial[1124:60b] File downloaded to: file:///var/mobile/Applications/2HH213HGH-2389-4AF6-7328-ESD213AD343S/Documents/angry.png
2014-03-20 00:30:48.185 ImageTutorial[1124:60b] File downloaded to: file:///var/mobile/Applications/2HH213HGH-2389-4AF6-7328-ED0FAF04A97E/Documents/shine.png
2014-03-20 00:30:48.194 ImageTutorial[1124:60b] File downloaded to: file:///var/mobile/Applications/2HH213HGH-2389-4AF6-7328-ESD213AD343S/Documents/relaxed.png
2014-03-20 00:30:48.196 ImageTutorial[1124:60b] File downloaded to: file:///var/mobile/Applications/2HH213HGH-2389-4AF6-7328-ESD213AD343S/Documents/bored.png
2014-03-20 00:30:48.219 ImageTutorial[1124:60b] File downloaded to: file:///var/mobile/Applications/2HH213HGH-2389-4AF6-7328-ESD213AD343S/Documents/happy.png
2014-03-20 00:30:48.233 ImageTutorial[1124:60b] File downloaded to: file:///var/mobile/Applications/2HH213HGH-8322-4AF6-7328-ESD213AD343S/Documents/sick.png
然后我将图像设置为UIImageView
self.IV_test.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@",[MUtility sharedObject].arrMoodImagePath[i]]]
最后,我无法设置图像。你有解决这个问题的办法吗?
【问题讨论】:
-
您是要创建一个新的 UIImageView 并将其放到屏幕上,还是只是更改屏幕上已经存在的 UIImageView 的图像?
-
我更改了代码,但它不再起作用了。
标签: ios objective-c uiimageview uiimage afnetworking