2013-05-18 5 views
0

나는 UIImageView 중 하나와 함께 매우 이상한 버그를 발견했습니다. 항목을 나열하는 UITableView이 있고 각 셀은 항목입니다. 각 항목 셀에는 축소판 이미지, 제목 및 부제목이 있습니다. 시뮬레이터와 iPhone에서 모든 셀의 미리보기 이미지를 완벽하게 표시 할 수 있습니다.UIImageView는 시뮬레이터에서 이미지를 보여 주지만 iPhone에서는 보이지 않습니다.

특정 셀을 두드리면 세그웨이가 해당 VC에 접속하여 큰 이미지와 아이템에 대한 추가 정보를 보여줍니다. 시뮬레이터에서 이미지를로드 할 수 있었지만 iPhone에서는 공백 인 것입니다. UIImageView.

이것은 시뮬레이터에서 보이는 방법입니다.

Table View

그리고 내 세부 VC : 나는이 항목에 대한 자세한 내용을 표시하는 라벨을 추가 할 경우

Detail VC

하단의 파란색보기, 그리고 오렌지는 UIImageView입니다 '항목 세부 정보'만 말하는 맞춤 이미지가 표시됩니다. 그 중 하나, 또는 큰 내 UImageView 그리고 그것을 움직일 수 있습니다 (그리고 아래로 해제) 최대 애니메이션 것입니다.

이제 문제는 - 내 VC에서 내 세부 VC로 연결될 때, UIImageView은 아이폰에서 그냥 비어 있습니다. 그것은 시뮬레이터에서 완벽하게 작동하지만 오른쪽 이미지 등을 전달합니다. 여기

코드가 SEGUE하는 것입니다

- (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]; 
     } 
    } 
} 

내 항목이 코어 데이터를 사용하여 저장됩니다. photoURL은 전체 이미지를 핵심 데이터로 저장하지 않기 때문에 내 앱의 문서 디렉토리에있는 사진의 URL이며 실적을 위해 참조로 사용됩니다.

"unwind"segue에서 다른 VC의 데이터를 가져 오는 코드의 일부입니다. 내 세부 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]]; 
} 

이 내가 큰 UIImageView에 보내는 UIImage를로드 할 모든 것입니다.

- (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); 
     } 
    } 
} 

이것과 나는 시뮬레이터에서 어떤 에러도 얻지 못한다.

누구든지 여기에 어떤 문제가 있는지 알고 계십니까? 감사.

답변

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 전에 접근 할 필요가로서 마지막으로는, 우리의 세부 VC 헤더 파일에, 우리는 UIImage (하지 UIImageView!), strong 참조 아닌 weak 하나가 될 필요가

가 화면에 :

@property (nonatomic, strong) UIImage *pictureImage; 

UIImageView 우리는보기가 화면에있는 경우에만 표시 할 필요가 있기 때문에이 UIImageweak을 할 수 있습니다에 할당하고, 그 서브 뷰의 모든에 strong 포인터를 보유하고 있습니다. strongweak의 우수 유추를 위해 check this.

+0

귀하의 질문에 답변 해 주셔서 +1합니다. 잘 했어.:) –

관련 문제