0

我对iOS开发很陌生。我的应用程序通过网络获取文件,将其写入 image.png,然后读取并显示图像。但是,显示部分无法正常工作,因为我的 UIImage 对象始终设置为 nil(在 iOS 模拟器上)。我试过从stackoverflow实现其他答案,但没有运气。

这是我保存文件的代码:

    //inside utility class for model
    NSFileHandle * handle = nil;

    //For first packet of new file request
    if(CountFileParts == 1)
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"image.png"];
        NSLog(@"%@",appFile);
        handle = [NSFileHandle fileHandleForWritingAtPath:appFile];
        if(handle == nil)
        {
            [[NSFileManager defaultManager] createFileAtPath:appFile contents:nil attributes:nil];
            handle = [NSFileHandle fileHandleForWritingAtPath:appFile];
        }
    }
    //For other incoming packets of the same request
    else
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"image.png"];
        //NSLog(@"%@",appFile);

        handle = [NSFileHandle fileHandleForUpdatingAtPath:appFile];
        [handle seekToEndOfFile];
        //NSLog(@"Writing continue in new file");

    }
    if(handle == nil)
        NSLog(@"handle nil");
    NSData * data = [str dataUsingEncoding:NSUTF8StringEncoding];
    [handle writeData:data];
    [handle closeFile];

    if(index != -1 && index!= NSNotFound)
    {
        NSLog(@"Inside Bool");
        self.isPlotReady = YES;//kvo in view-controller as shown below
        self.isPlotReady = NO;
    }

这是我加载图像文件的代码:

-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary  *)change context:(void *)context

{
    if([keyPath isEqualToString:@"isPlotReady"])
    {
        self.isReady = [[change objectForKey:NSKeyValueChangeNewKey] boolValue];
        [self updateUI];
    }
}
-(void) updateUI
{
    if(self.isReady)
    {
       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
       NSString *documentsDirectory = [paths objectAtIndex:0];
       [self lsOwnDirectory:documentsDirectory];
       NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"image.png"];
       //NSLog(@"%@",appFile);
       UIImage *img = [UIImage imageWithContentsOfFile:appFile];

       if(img == nil)
          NSLog(@"Couldn't find image");
       else
       {
          UIImageView *imageView = [[UIImageView alloc] initWithImage:img] ;
          [self.view addSubview:imageView];
       }
   }
}
//Prints Directory contents of input directory
- (void) lsOwnDirectory:(NSString *) currentpath {
    NSError * error = [[NSError alloc] init];
    NSFileManager *filemgr;

    filemgr = [[NSFileManager alloc] init];

    //currentpath = [filemgr currentDirectoryPath];
    NSLog(@"Current Directory Path : %@",currentpath);
    NSArray * files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:currentpath error: &error];

    for(NSString * file in files){
      NSLog(@"%@", file);
    }
}

它总是打印与 if 语句相对应的“找不到图像”,但我看到该文件仍然存在(lsOwnDirectory 打印目录内容)。也许我在这里做一些基本的错误。提前致谢。

4

0 回答 0