取本地相册里面的照片并上传

首先的导入要UIImagePickerControllerDelegate,UINavigationControllerDelegate者两个协议

UIActionSheet *_actionSheet = [[UIActionSheet alloc]initWithTitle:@"选择方式" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册选择",@"现在拍摄", nil];


#pragma mark 选择图片方式

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{

    UIImagePickerController * imagePickerController = [[UIImagePickerController alloc]init];

    

    imagePickerController.delegate = self;

    

    if (buttonIndex == 0) {

        //从相册选

        imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        [self presentViewController:imagePickerController animated:YES completion:^{

        }];

    }else if (buttonIndex == 1){

        //现在拍摄

        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;

        [self presentViewController:imagePickerController animated:YES completion:^{

        }];

    }else if(buttonIndex == 2){

        //取消

        [_actionSheet removeFromSuperview];

    }

}

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

      if (indexPath.row == _imageArr.count-1 && !_imageFull) {//选择图片

    [_actionSheet showInView:self.view];

        }

}

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

    [self dismissViewControllerAnimated:YES completion:^{

        

    }];

    UIImage * image = [BaseView imageWithImage:[info objectForKey:@"UIImagePickerControllerOriginalImage"] scaledToSize:CGSizeMake(200, 200)];

    UIImage * upImage = [info objectForKeyedSubscript:@"UIImagePickerControllerOriginalImage"];

    

    [_imageArr insertObject:image atIndex:0];

//    [_imageDataArr insertObject:UIImageJPEGRepresentation(upImage, 0.3) atIndex:0];

    NSData *data = UIImageJPEGRepresentation(upImage, 0.3);


   

    

    

    //      图片的保存路径

    NSString *documentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

    NSFileManager *manager = [NSFileManager defaultManager];

    

    //      将刚刚转化的图片放到沙盒中 并保存为png

    [manager createDirectoryAtPath:documentsPath withIntermediateDirectories:YES attributes:nil error:nil];

    NSString *imageName = [NSString stringWithFormat:@"/%@.png",[NSDate date]];

    

    [manager createFileAtPath:[documentsPath stringByAppendingString:[NSString stringWithFormat:@"%@",imageName]] contents:data attributes:nil];

    

    //得到选择后沙盒的路径

    filePath = [[NSString alloc]initWithFormat:@"%@%@",documentsPath,imageName];

    NSLog(@"filePath:%@",filePath);

    [picker dismissViewControllerAnimated:YES completion:nil];

    [_imageDataArr addObject:filePath];

    if (_imageDataArr.count == 3) {

        [_imageArr removeObjectAtIndex:3];

        _imageFull = YES;

    }

    

}

- (void)issue

{

    [BaseView showHUD:@"正在发表..." andView:self.view andHUD:_hud];


    article = [BmobObject objectWithClassName:@"Article"];

        if (_imageArr.count > 1) {

//            上传图片

            [BmobFile filesUploadBatchWithPaths:_imageDataArr progressBlock:^(int index, float progress) {

                

            } resultBlock:^(NSArray *array, BOOL isSuccessful, NSError *error) {

                 NSMutableArray *images = [NSMutableArray array];

                for (BmobFile *file in array) {

                    

                    [images addObject:file.url];

                }

                BmobObject *object = [BmobObject objectWithClassName:@"Article"];

                [object addObjectsFromArray:images forKey:@"images"];

                UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];

                          [object saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {

                              if (isSuccessful) {

                                  alert.tag = 10;

                                  [alert setMessage:@"发表成功"];

                                  [alert show];

                                  [_hud hide:YES];

                              }

                          }];

            }];

            

    }

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值