0

我的一个UIImageViews 遇到了一个非常奇怪的错误。我有一个UITableView列出项目的列表,每个单元格都是一个项目。每个项目单元格都有一个缩略图、一个标题和一个副标题。我能够在模拟器和 iPhone 上完美地显示每个单元格中的缩略图。

我还有一个详细的 VC,当某个单元格被点击时,它会转到该 VC 并显示大图像,以及有关该项目的更多信息。我已经能够在模拟器中加载图像,但在 iPhone 上,我得到的只是一个空白UIImageView.

这就是它在模拟器中的样子。

表视图

还有我的详细 VC:

详细VC

底部的蓝色视图是我将添加标签以显示有关项目的更多详细信息的位置,橙色视图UIImageView将显示自定义图像,仅显示“项目详细信息”。一个人可以点击它,或者我的大UImageView,它会向上(和向下关闭)动画。

现在,问题是 - 当我从我的表 VC 转到我的详细 VC 时,UIImageViewiPhone 上只是空白。不过,它在模拟器上工作得非常好,所以我传递了正确的图像等。

这是要继续的代码:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"showItemDetails"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        ItemDetailViewController *itemDetailVC = segue.destinationViewController;
        Item *item = [self.fetchedResultsController objectAtIndexPath:indexPath];
        if(item)
        {
            itemDetailVC.pictureImage = [UIImage imageWithContentsOfFile:item.photoURL];
        }
    }
}

我的项目使用 Core Data 保存。photoURL是我的应用程序文档目录中照片的 URL,因为我没有将整个图像保存到 Core Data,只是出于性能目的对其进行引用。

这是从另一个 VC 在“展开”segue 中获取数据的部分代码。

- (IBAction)saveItem:(UIStoryboardSegue *)segue
{
    ManageItemViewController *manageItemVC = (ManageItemViewController *)segue.sourceViewController;

    NSMutableDictionary *itemInfo = [[NSMutableDictionary alloc] init];

    ...

    // write the original photo to a file and keep an URL to it that we pass to Core Data
    NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(manageItemVC.pictureImage)];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
    NSString *fileName = [NSString stringWithFormat:@"%lf.png", [[NSDate date] timeIntervalSince1970]];
    NSString *filePath = [documentsPath stringByAppendingPathComponent:fileName]; //Add the file name
    [imageData writeToFile:filePath atomically:YES]; //Write the file

    [itemInfo setObject:filePath forKey:CD_ITEM_PHOTOURL_PROPERTY];

    ...

    [self saveItemToCoreData:[itemInfo copy]];
}

在我的详细 VC 中,这就是我加载UIImage发送到大的UIImageView.

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if(self.pictureImage) {
        [self.pictureImageView setImage:self.pictureImage];
    }
} 

当用户删除一个项目(删除行​​)时,我也会从我的文档目录中删除该文件(此代码在我的表 VC 中):

- (void)deleteItemImageFromDevice:(NSString *)filePath
{
    if(filePath)
    {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error;
        if([fileManager removeItemAtPath:filePath error:&error]) {
            NSLog(@"Image deleted from documents directory!");
        } else {
            NSLog(@"%@", error);
        }
    }
}

我记录了这个,我在模拟器中没有得到任何错误。

有人知道这里有什么问题吗?谢谢。

4

1 回答 1

1

我已经设法解决了这个问题。

为了将来参考,这里是我对我的代码所做的修复。

而不是时间间隔,我现在使用 得到保证唯一的字符串(不需要长浮点转换)NSProcessInfo,如下所示:

NSString *fileName = [NSString stringWithFormat:@"%@.png", [[NSProcessInfo processInfo] globallyUniqueString]];

然后,在我的prepareForSegue:方法中,我UIImage从存在于我们的文档文件夹(文件)中的数据中获取。这item.photoURL是我们的文件路径。

Item *item = [self.fetchedResultsController objectAtIndexPath:indexPath];
        if(item.photoURL)
        {
            itemDetailVC.pictureImage = [UIImage imageWithData:[NSData dataWithContentsOfFile:item.photoURL]];
        }

最后,在我们详细的 VC 头文件中,我们的UIImage(不是UIImageView!)需要被strong引用,而不是weak一个,因为我们需要在 VC 出现在屏幕上之前访问它:

@property (nonatomic, strong) UIImage *pictureImage;

UIImageView我们将其分配给UIImage可以是weak因为它只需要在视图在屏幕上时显示,并且它包含strong指向其所有子视图的指针。对于strongvs的一个很好的类比weak检查这个

于 2013-05-18T01:16:47.130 回答