【发布时间】: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
datasource和delegate设置为tableViewCell? -
@KKRocks 不,我不需要保存图像,只需选择它们并上传到服务器。
-
@Rishab 是的,我是
标签: ios objective-c uicollectionview uiimagepickercontroller