【问题标题】:Get image from File in iOS在 iOS 中从文件中获取图像
【发布时间】: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


【解决方案1】:

您似乎将ImageViewUIImage 混淆了。您正在将 UIImage 的实例分配给 UIImageView 变量。

UIImageView *imgView = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@",[MUtility sharedObject].arrMoodImagePath[i]]]

您需要做的是如何获取屏幕上的视图实例并将图像分配给它。类似的东西

UIImageView *imgView = //... get instance from screen, alloc init, NIB, storyboard, ...
imgView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@",[MUtility sharedObject].arrMoodImagePath[i]]]

【讨论】:

    【解决方案2】:

    把你的最后陈述分成几部分。将路径名提取为字符串。记录下来。

    将该路径中的图像加载到局部变量中的 UIImage 中。记录下来。

    然后,将图像安装到图像视图中。如果这不起作用,请检查您的图像视图属性是否为零。 (断开的出口链接是代码无法按预期工作的常见原因)

    【讨论】:

      猜你喜欢
      • 2014-01-12
      • 1970-01-01
      • 2011-05-31
      • 1970-01-01
      • 2012-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-04
      相关资源
      最近更新 更多