0

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

非常感谢任何帮助。

4

1 回答 1

1

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

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

    } 
于 2015-07-14T13:36:26.083 回答