【问题标题】:Add images from UIImagePickerViewController to UICollectionView inside UITableViewCell将 UIImagePickerViewController 中的图像添加到 UITableViewCell 内的 UICollectionView
【发布时间】:2017-05-31 04:32:01
【问题描述】:


首先,很抱歉我的长问题。
我正在创建一个带有 UICollectionView 的自定义 UITableViewCell 。每当用户选择此单元格时,都会出现 UIImagePickerController,现在我想将 UIImagePickerController 中的所有选定图像传递到我的自定义单元格内的 UICollectionView 中。
我已完成显示图像选择器,现在我的问题是在集合视图中显示选定的图像。
我怎样才能做到?
我的代码:

SHImageUploadCell.h

#import <UIKit/UIKit.h>
@class SHImageUploadCell;
@protocol SHImagUploadDelegate<NSObject>
@optional
- (void)closeImage:(SHImageUploadCell*)cell;
@end
@interface SHImageUploadCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *imgUpload;
@property (strong,nonatomic) id<SHImagUploadDelegate> delegate;
@property(nonatomic) int imageIndex;
@property (weak, nonatomic) IBOutlet UIImageView *bugImage;

@end

SHImageUploadCell.m

#import "SHImageUploadCell.h"

@implementation SHImageUploadCell
- (IBAction)bntDeleteImageClick:(id)sender {


    NSDictionary* userInfo = @{@"index": @(self.imageIndex)};

    NSLog(@"%d", self.imageIndex);
    if (_delegate) {
        [_delegate closeImage:self];
    }
}

@end

MyCustomCell.h

#import <UIKit/UIKit.h>

@interface SHPhotoPickerViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UICollectionView *photoCollection;
@property (strong, nonatomic) NSMutableArray *listImage;
@end

MyCustomCell.m

#import "SHPhotoPickerViewCell.h"
#import "SHImageUploadCell.h"

@interface SHPhotoPickerViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, SHImagUploadDelegate>

@end

@implementation SHPhotoPickerViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self.photoCollection registerClass:[SHImageUploadCell class] forCellWithReuseIdentifier:@"selectedCell"];
    [self.photoCollection registerNib:[UINib nibWithNibName:@"SHImageUploadCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"selectedCell"];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.listImage.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SHImageUploadCell *uploadCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"uploadCell" forIndexPath:indexPath];
    uploadCell.imgUpload.image = [UIImage imageWithData: self.listImage[indexPath.row]];
    uploadCell.imageIndex = (int)indexPath.row;
    uploadCell.delegate=self;
    return uploadCell;
}

@end

处理单元格选择:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 4){
   UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
   imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
   imagePickerController.delegate = self;
   [self presentViewController:imagePickerController animated:YES completion:nil];
    }
  }
}

UIImagePickerController 委托:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    SHPhotoPickerViewCell *photoPickerCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.pickerCellIndexPath.row inSection:1]];
    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
    image = [SHHelper resizeImageWithImage:image toSize:CGSizeMake(480, 320)];
    NSData *imageData = UIImageJPEGRepresentation(image, 0.7);
    photoPickerCell.listImage = [NSMutableArray new];
    [photoPickerCell.listImage addObject:imageData];

    [photoPickerCell.photoCollection reloadData];

    [picker dismissViewControllerAnimated:YES completion:^{

    }];


}

【问题讨论】:

  • 你需要在self.listImage中添加url然后重新加载你的tableview和collectionview
  • 你需要保存这张图片吗?
  • 您是否将collectionview datasourcedelegate 设置为tableViewCell?
  • @KKRocks 不,我不需要保存图像,只需选择它们并上传到服务器。
  • @Rishab 是的,我是

标签: ios objective-c uicollectionview uiimagepickercontroller


【解决方案1】:

试试这个:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    id object = [listImage objectAtIndex:indexPath.row];
    if ([object isKindOfClass:[UIImage class]]) {
        uploadCell.imgUpload.image = (UIImage *)object;
    }
    else{
       uploadCell.imgUpload.image = [UIImage imageWithData: object];
    }

       return uploadCell;
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
    [listImage addObject:image]; // here add image which you selected
     // reload tableview
     // reload collection view
    [picker dismissViewControllerAnimated:YES completion:^{

    }];


}

【讨论】:

  • 没有任何变化。当我选择图像时它仍然崩溃:[UITableViewCell listImage]: unrecognized selector sent to instance 0x7fcd3b897c00
  • [photoPickerCell.photoCollection reloadData];线
  • 您无法使用 cell.collection 重新加载。删除该行。是否有一个或多个集合视图?
  • 或者只重新加载tableview。
  • 我必须说你弄错了单元格。所以首先你需要检查细胞类型。因为在 didSelectRowAtIndexPath 方法上有“indexPath.row == 4”,但是您在 didFinishPickingMediaWithInfo 方法上使用了“section: 1”。
猜你喜欢
  • 2017-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-10
相关资源
最近更新 更多