の1 つに非常に奇妙なバグが発生していUIImageView
ます。アイテムをUITableView
リストする があり、各セルはアイテムです。各アイテム セルには、サムネイル イメージ、タイトル、およびサブタイトルがあります。シミュレーターとiPhoneで、すべてのセルにサムネイルを完全に表示できます。
また、特定のセルがタップされると、その VC にセグエされ、アイテムに関する詳細情報とともに大きな画像が表示される詳細 VC もあります。シミュレーターで画像を読み込むことができましたが、iPhone では空白のUIImageView
.
シミュレーターで見るとこんな感じ。
そして私の詳細VC:
下部の青いビューは、アイテムの詳細を表示するラベルを追加する場所です。オレンジ色のビューは、UIImageView
「アイテムの詳細」とだけ表示されるカスタム イメージを表示します。それをタップするか、私の大きなものをタップすると、上UImageView
にアニメーション化されます(閉じるには下に移動します)。
さて、問題は、テーブル VC から詳細 VC にセグエするとUIImageView
、iPhone で空白になることです。ただし、シミュレーターでは問題なく動作するため、正しい画像などを渡しています。
セグエするコードは次のとおりです。
- (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 からデータを取得するコードの一部を次に示します。
- (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);
}
}
}
これをログに記録しましたが、シミュレーターでエラーは発生しません。
ここで何が問題なのか知っている人はいますか?ありがとう。