我试图从下载的文件中获取图像。首先,我获取图像并下载它们。
-(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]]]
最后,我无法设置图像。你有解决这个问题的方法吗?
最佳答案
您似乎混淆了 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]]]
关于ios - 在 iOS 中从文件中获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22519913/