我正在构建一个项目,我在其中使用 ELCImagepickerController 进行多项选择,然后将这些选定的图像保存到核心数据中。现在我在另一个 VC 中获取这些图像并将它们显示到 UICollectionView 中。
但是我在这里面临的问题是图像在 collectionView 中没有正确显示。我有两个困惑。
- 图像是否正确保存到核心数据中。
- 如果图像已保存,则是否正确获取。
在这里,我将选定的图像输入核心数据
- (void)imagePicker:(SNImagePickerNC *)imagePicker didFinishPickingWithMediaInfo:(NSMutableArray *)info
{
_arrImages = [[NSMutableArray alloc]init];
_imgdata = [[NSMutableData alloc]init];
AppDelegate* app=(AppDelegate*)[[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context = app.managedObjectContext;
Albums *objPhotos = (Albums *)[NSEntityDescription insertNewObjectForEntityForName:@"Albums" inManagedObjectContext:context];
//get images
for (int i = 0; i < info.count; i++) {
ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
[assetLibrary assetForURL:info[i] resultBlock:^(ALAsset *asset) {
UIImage *image = [UIImage imageWithCGImage:[asset aspectRatioThumbnail]];
_imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
[objPhotos setPhotosInAlbum:self.imageData];
} failureBlock:^(NSError *error) { }];
}
NSError * err = nil;
[context save:&err];
if (![context save:&err]) {
NSLog(@"Can't Save! %@ %@", err, [err localizedDescription]);
}
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Success!!" message:@"Photos Successfully Selected!" delegate:self cancelButtonTitle:@"DONE" otherButtonTitles:nil];
[alert show];
}
在这里,我正在获取那些选定的图像
-(NSArray *)getMenCategoryList
{
AppDelegate* app=(AppDelegate*)[[UIApplication sharedApplication]delegate];
NSManagedObjectContext *moc=app.managedObjectContext;
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Albums" inManagedObjectContext:moc];
[fetch setEntity:entity];
NSError *error;
NSArray *result = [moc executeFetchRequest:fetch error:&error];
if (!result) {
return nil;
}
return result;
}
我正在调用获取函数- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
并返回数组计数
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [[self getMenCategoryList] count];
}
最后,在此处将图像填充到 collectionView 中
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"albumCollectionViewCellCell" forIndexPath:indexPath];
Photos *objAlbumPhotos = (Photos *)[[self getMenCategoryList] objectAtIndex:indexPath.row];
UIImage *albumImg = [UIImage imageWithData:[objAlbumPhotos valueForKey:@"photosInAlbum"]];
UIImageView *photosimageview = (UIImageView *)[cell.contentView viewWithTag:1];
photosimageview.image = albumImg;
return cell;
}
非常感谢任何帮助。