현재 웹에서 NSManagedObject 클래스로 이미지를 저장하려고합니다. 응용 프로그램이 열릴 때마다 새로 다운로드 할 필요가 없습니다. 나는 현재이 두 클래스를 가지고있다.CoreData의 NSData가 제대로 저장되지 않습니다.
Plant.h
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) PlantPicture *picture;
PlantPicture.h
@property (nonatomic, retain) NSString * bucketName;
@property (nonatomic, retain) NSString * creationDate;
@property (nonatomic, retain) NSData * pictureData;
@property (nonatomic, retain) NSString * slug;
@property (nonatomic, retain) NSString * urlWithBucket;
는 이제 다음을 수행하십시오
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
PlantCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
Plant *plant = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.plantLabel.text = plant.name;
if(plant.picture.pictureData == nil)
{
NSLog(@"Downloading");
NSMutableString *pictureUrl = [[NSMutableString alloc]initWithString:amazonS3BaseURL];
[pictureUrl appendString:plant.picture.urlWithBucket];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:pictureUrl]];
AFImageRequestOperation *imageOperation = [AFImageRequestOperation imageRequestOperationWithRequest:request success:^(UIImage *image) {
cell.plantImageView.image = image;
plant.picture.pictureData = [[NSData alloc]initWithData:UIImageJPEGRepresentation(image, 1.0)];
NSError *error = nil;
if (![_managedObjectContext save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
}];
[imageOperation start];
}
else
{
NSLog(@"Already");
cell.plantImageView.image = [UIImage imageWithData:plant.picture.pictureData];
}
NSLog(@"%@", plant.name);
return cell;
}
공장 정보가 존재뿐만 아니라 그림 개체입니다. 그러나 NSData 자체는 응용 프로그램을 열고 닫을 때 절대로 저장되지 않습니다. 항상 이미지를 다운로드해야합니다! 어떤 아이디어!? [CoreData를 처음 접 하셨다면 ... 미안합니다.]
감사합니다!
편집 & 업데이트 :
이미지 다운로드가 전무되지 않고 다운로드 후 잘 나타납니다. UIImageJPEGRepresentation에 의해 반환 된 데이터가 nil이 아닌 것처럼 보입니다. 내 managedObjectContext도 nil이 아닙니다. 요청은 내 디버거에 따라 주 스레드에서 수행됩니다. 내가 확인해야 할 다른 건?!
또한보기를 종료하고 돌아 오면 이미지가 다시 다운로드되지 않으며 로그가 표시되고 "이미"로그가 나타납니다. 그러나 응용 프로그램을 닫았다가 다시 열면 다시 다운로드해야합니다. 나머지 CoreData가 여전히 남아 있기 때문에 이상하게 보입니다.
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
업데이트 # 2
내 모든 ManagedObjectContext가 동일한 것으로 보인다. 캐시와 관련이 있습니까?
정보를 더 제공 할 수 있습니까? 다운로드 한 이미지가 0이 아닙니다. 완료 블록이 백그라운드 스레드 또는 메인 스레드에서 처리됩니까? _managedObjectContext 확실히 nil 아닌가요? 그리고 조심해서 UIImageJPEGRepresentation()은 011을 반환 할 것입니다 ... – Alexander
편집을 참조하십시오! 감사합니다 :) – abisson
흠 당신 fetledResultsController 동일한 managedObjectContext를 사용하고 있습니까?나는 당신이 _moc을 사용하여 한 번 액세스했기 때문에 self.moc를 통해 resultsController를 생성했기 때문에 묻고있다. – Alexander