ios开发--弹出相机相册相关操作

ios开发--弹出相机相册相关操作_第1张图片
13865632,1366,768.jpg

今天写道需要选取照片(可以拍照或者选取图库的),不废话,上代码
这个是点击事件

//提交选项
-(void)sendDataAction:(UIButton *) button{

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"从相册选择", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view];

}
//这里做个判断

  • (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    {
    if (buttonIndex == 0) {
    [self toCameraPickingController];
    }
    else if (buttonIndex == 1) {
    [self toPhotoPickingController];
    }

}
//相机判断

  • (void)toCameraPickingController
    {
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
    NSLog(@"Error:没有照相设备");
    }
    else {
    UIImagePickerController *cameraPicker = [[UIImagePickerController alloc] init];
    cameraPicker.delegate = self;
    cameraPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([self respondsToSelector:@selector(presentViewController:animated:completion:)]) {
    [self presentViewController:cameraPicker animated:YES completion:nil];
    }
    else {
    // [self presentModalViewController:cameraPicker animated:YES];这个是找的demo里面的,这个方法过时了,被下面的替代了
    [self presentViewController:cameraPicker animated:YES completion:nil];

      }
      [_sendDataButt setTitle:@"已上传" forState:UIControlStateNormal];
    

    }
    }
    //图库判断

  • (void)toPhotoPickingController
    {
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
    NSLog(@"Error:无图片库");
    }
    else {
    UIImagePickerController *photoPicker = [[UIImagePickerController alloc] init];
    photoPicker.delegate = self;
    photoPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    if ([self respondsToSelector:@selector(presentViewController:animated:completion:)]) {
    [self presentViewController:photoPicker animated:YES completion:nil];
    }
    else {
    // [self presentModalViewController:photoPicker animated:YES];这个是找的demo里面的,这个方法过时了,被下面的替代了
    [self presentViewController:photoPicker animated:YES completion:nil];
    }
    [_sendDataButt setTitle:@"已上传" forState:UIControlStateNormal];

    }
    }

pragma mark UIImagePickerControllerDelegate Methods

//图片选择

  • (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
    if ([self respondsToSelector:@selector(dismissViewControllerAnimated:completion:)]) {
    [picker dismissViewControllerAnimated:YES completion:nil];
    }
    else {
    //[picker dismissModalViewControllerAnimated:YES];这个是找的demo里面的,这个方法过时了,被下面的替代了
    [self dismissViewControllerAnimated:YES completion:nil];
    }
    UIImage *imagePicked = [info objectForKey:UIImagePickerControllerOriginalImage];
    _sendImage.image = imagePicked;
    }

pragma mark UIImagePickerControllerDelegate Cancel Methods

  • (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
    {
    //[picker dismissModalViewControllerAnimated:YES];这个是找的demo里面的,这个方法过时了,被下面的替代了
    [self dismissViewControllerAnimated:YES completion:nil];
    }
    //设置一下屏幕
  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }


补充一下,携带参数上传

  • (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
    NSLog(@"图片选中");
    //截取图片
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    NSData *imageData = UIImageJPEGRepresentation(image, 0.001);
    self.mv.portraitImageView.image = image;

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", nil nil];
    // 参数
    NSMutableDictionary *parameter = [NSMutableDictionary dictionary];
    parameter[@"token"] = "param....";
    // 访问路径
    NSString *stringURL = [NSString stringWithFormat:@"%@%@",HOSTURL,kUploadAvatar];

    [manager POST:stringURL parameters:parameter constructingBodyWithBlock:^(id formData) {
    // 上传文件
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyyMMddHHmmss";
    NSString *str = [formatter stringFromDate:[NSDate date]];
    NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];

      [formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/png"];  
    

    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"上传成功");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"上传错误");
    }];
    }

ios开发--弹出相机相册相关操作_第2张图片
13865647,1366,768.jpg

你可能感兴趣的:(ios开发--弹出相机相册相关操作)