我试图从下载的文件中获取图像。首先我得到图像并下载它们。
-(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]]]
最后,我无法设置图像。你有解决这个问题的方法吗?
答案 0 :(得分:1)
您似乎将ImageView
与UIImage
混淆了。您正在将UIImage
的实例分配给UIImageView
变量。
UIImageView *imgView = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@",[MUtility sharedObject].arrMoodImagePath[i]]]
您需要做的是如何获取屏幕上的View实例并将图像分配给它。像
这样的东西UIImageView *imgView = //... get instance from screen, alloc init, NIB, storyboard, ...
imgView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@",[MUtility sharedObject].arrMoodImagePath[i]]]
答案 1 :(得分:1)
将最终陈述分成几部分。将路径名解压缩到字符串。记录下来。
将该路径上的图像加载到局部变量中的UIImage中。记录下来。
然后,将图像安装到图像视图中。如果这不起作用,请检查您的图像视图属性是否为零。 (断开的出口链接是代码无法按预期工作的常见原因)