UIImage给出了无

时间:2013-06-13 11:08:02

标签: objective-c file uiimage

我对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打印目录内容)。也许我在这里做了一些基本的错误。提前谢谢。

0 个答案:

没有答案