起初,对于我长期的问题感到抱歉。
我正在用UICollectionView在里面创建一个自定义的UITableViewCell。每当用户选择这个单元格时,UIImagePickerController就会出现,现在我想将所有选定的图像从UIImagePickerController传递到我的自定义单元格内的UICollectionView。
我已完成显示图像选择器,现在我的问题是在集合视图中显示选定的图像。
我该怎么做?
我的代码:
将UIImagePickerViewController中的图像添加到UITableViewCell中的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 – KKRocks
你需要s这个图像? – KKRocks
你有没有为tableViewCell设置collectionview'datasource'和'delegate'? – Rishab