iOS

发表评论页面(星级,文本,图片)二

2017-04-26  本文已影响13人  小柴2011

直接上代码

//需要遵守的代理方法

UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate,UICollectionViewDelegate,UICollectionViewDataSource

//创建属性

@property(nonatomic,strong)NSMutableArray*uploadImageArray;

@property(nonatomic,strong)NSMutableArray*tempImageArray;

@property(nonatomic,strong)UICollectionView*myCollectionView;

//下面这几行代码写在- (void)viewDidLoad {}里

//camera是相机图片的名字

- (void)viewDidLoad {

//**********************************************************************上传图片****************************************************************************//

self.uploadImageArray= [NSMutableArraynew];

[self.uploadImageArrayaddObject:[UIImageimageNamed:@"camera"]];

[self addImage];

}

//我的需求是上传三张图片 ,当然该方法也可以实现任意多张。图片的布局是一个UICollectionView

-(void)addImage

{

NSLog(@"%ld",self.uploadImageArray.count);

UICollectionViewFlowLayout*layout = [[UICollectionViewFlowLayoutalloc]init];

layout.itemSize=CGSizeMake(60,60);

layout.sectionInset=UIEdgeInsetsMake(1,1,1,1);

layout.minimumInteritemSpacing=1;

layout.minimumLineSpacing=1;

self.myCollectionView= [[UICollectionViewalloc]initWithFrame:CGRectMake(20,340,kScreenWidth-40,60)collectionViewLayout:layout];

self.myCollectionView.delegate=self;

self.myCollectionView.dataSource=self;

self.myCollectionView.backgroundColor= [UIColorclearColor];

self.myCollectionView.scrollEnabled=NO;

//注册item

[self.myCollectionViewregisterClass:[UICollectionViewCellclass]forCellWithReuseIdentifier:@"kitemCellid"];

[self.viewaddSubview:self.myCollectionView];

[self.myCollectionViewreloadData];

}

//

#pragma mark - UICollectionViewDelegate , UICollectionViewDataSource 代理方法

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{

return1;

}

- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section

{

NSLog(@"shuchu===%@",_uploadImageArray);

return_uploadImageArray.count;

}

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

{

NSLog(@"选择的图片%ld",self.uploadImageArray.count );

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"kitemCellid"forIndexPath:indexPath];

UIImageView*image = [[UIImageViewalloc]initWithImage:[self.uploadImageArrayobjectAtIndex:indexPath.row]];

image.frame=CGRectMake(0,0,59,59);

[_bottomViewaddSubview:image];

image.userInteractionEnabled=YES;

[celladdSubview:image];

//

UIButton*deleteBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

UIImage*images = [UIImageimageNamed:@"deleteimage"];

deleteBtn.frame=CGRectMake(image.frame.size.width- images.size.width, image.frame.origin.y, images.size.width, images.size.height);

[deleteBtnsetImage:imagesforState:UIControlStateNormal];

[deleteBtnaddTarget:selfaction:@selector(deleteAction:)forControlEvents:UIControlEventTouchUpInside];

deleteBtn.tag=3600+ indexPath.row;

[_bottomViewaddSubview:deleteBtn];

if(indexPath.row==3) {

image.userInteractionEnabled=NO;

}

if(indexPath.row==self.uploadImageArray.count-1) {

deleteBtn.hidden=YES;

}

[celladdSubview:deleteBtn];

returncell;

}

- (void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath

{

NSLog(@"点击图片");

if(indexPath.row==self.uploadImageArray.count-1) {

if(indexPath.row>=3) {

NSLog(@"提示用户最多添加三张图片");

[selfshowAlert:@"最多上传3张图片"];

}else{

UIActionSheet*sheet;

// 判断是否支持相机

if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

{

sheet= [[UIActionSheetalloc]initWithTitle:nildelegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"拍照",@"从手机相册选择",nil];

sheet.tag=2255;

}else{

sheet = [[UIActionSheetalloc]initWithTitle:nildelegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"从手机相册选择",nil];

sheet.tag=3255;

}

[sheetshowInView:self.tabBarController.view];

}

}else{

NSLog(@"编辑图片");

DFWEditImageViewController*vc = [[DFWEditImageViewControlleralloc]init];

vc.imageArry=self.tempImageArray;

[self.navigationControllerpushViewController:vcanimated:YES];

}

}

- (void)deleteAction:(UIButton*)sender

{

NSLog(@"%@",_uploadImageArray);

self.imgUrlArray= (NSMutableArray*)[[_imgUrlArrayreverseObjectEnumerator]allObjects];

NSLog(@"%ld", sender.tag);

if(self.imgUrlArray.count== sender.tag-3600+1) {

[self.imgUrlArrayremoveObjectAtIndex:sender.tag-3600];

[self.uploadImageArrayremoveObjectAtIndex:sender.tag-3600];

[self.tempImageArrayremoveObjectAtIndex:sender.tag-3600];

}else{

//dispatch_queue_t queue =dispatch_queue_create("deleteImage",NULL);

//dispatch_async(queue, ^{

//[self.imgUrlArray removeObjectAtIndex:sender.tag - 3600];

//});

NSLog(@"下载中");

}

NSLog(@"%@",_uploadImageArray);

[self.myCollectionViewreloadData];

}

//调用相机方法

- (void)takeCamera:(UIButton*)sender

{

UIImagePickerControllerSourceTypesourceType =UIImagePickerControllerSourceTypeCamera;

if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

{

UIImagePickerController*picker = [[UIImagePickerControlleralloc]init];

picker.delegate=self;

//设置拍照后的图片可被编辑

picker.allowsEditing=YES;

picker.sourceType= sourceType;

[selfpresentViewController:pickeranimated:YEScompletion:nil];

}else

{

NSLog(@"模拟其中无法打开照相机,请在真机中使用");

}

}

//相机代理方法

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)image editingInfo:(nullableNSDictionary *)editingInfoNS_DEPRECATED_IOS(2_0,3_0)

{

if(editingInfo) {

NSLog(@"%@", editingInfo);

[self.uploadImageArrayaddObject:image];

[self.myCollectionViewreloadData];

[selfuploadImg:image];

[self.tempImageArrayaddObject:image];

self.uploadImageArray= (NSMutableArray*)[[_uploadImageArrayreverseObjectEnumerator]allObjects];

}else{

NSLog(@"取消");

}

[pickerdismissViewControllerAnimated:YEScompletion:nil];

}

//取消拍摄

- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker

{

NSLog(@"取消拍摄");

self.uploadImageArray= (NSMutableArray*)[[_uploadImageArrayreverseObjectEnumerator]allObjects];

[pickerdismissViewControllerAnimated:YEScompletion:nil];

}

//打开相册方法

- (void)openAlbum:(UIButton*)sender

{

UIImagePickerController*pickerImage = [[UIImagePickerControlleralloc]init];

if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

pickerImage.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;

pickerImage.mediaTypes= [UIImagePickerControlleravailableMediaTypesForSourceType:pickerImage.sourceType];

}

pickerImage.delegate=self;

pickerImage.allowsEditing=NO;

[selfpresentViewController:pickerImageanimated:YEScompletion:^{}];

}

//相册代理方法

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info

{

UIImage*image = info[@"UIImagePickerControllerOriginalImage"];

if(info) {

NSLog(@"%@", info);

[self.uploadImageArrayaddObject:image];

[self.myCollectionViewreloadData];

[selfuploadImg:image];

[self.tempImageArrayaddObject:image];

self.uploadImageArray= (NSMutableArray*)[[_uploadImageArrayreverseObjectEnumerator]allObjects];

}else{

NSLog(@"取消");

}

[pickerdismissViewControllerAnimated:YEScompletion:nil];

}

上一篇下一篇

猜你喜欢

热点阅读