gpt4 book ai didi

ios - 在核心数据的库中保存和获取多个图像选择

转载 作者:行者123 更新时间:2023-11-29 01:51:04 25 4
gpt4 key购买 nike

我正在构建一个项目,我在其中使用 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;
}

非常感谢任何帮助。

最佳答案

注意:我只是编辑您的代码,请先根据您的代码确认,然后根据您的要求进行编辑

第 1 步:生成唯一的文件名并将图像以该文件名保存到您的文件路径。
步骤 2:成功写入文件后,将该路径保存到核心数据。
步骤3:为了获取,使用核心数据路径从目录中获取图像。

- (NSString*)generateFileNameWithExtension:(NSString *)extensionString {
// Extenstion string is like @".png"

NSDate *time = [NSDate date];
NSDateFormatter* df = [NSDateFormatter new];
[df setDateFormat:@"dd-MM-yyyy-hh-mm-ss"];
NSString *timeString = [df stringFromDate:time];
int r = arc4random() % 100;
int d = arc4random() % 100;
NSString *fileName = [NSString stringWithFormat:@"File-%@%d%d%@", timeString, r , d , extensionString ];

NSLog(@"FILE NAME %@", fileName);

return fileName;
}



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

NSString *strfilename = [self generateFileNameWithExtension:@".jpg"];


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:strfilename];

[_imageData writeToFile:filePath atomically:YES];

[objPhotos setPhotosInAlbum:filePath];

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

}

关于ios - 在核心数据的库中保存和获取多个图像选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31407388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com