首先的导入要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];
}
}];
}];
}
}