美文网首页
iPad图片上传的方法

iPad图片上传的方法

作者: 左方 | 来源:发表于2019-11-21 11:59 被阅读0次

    实现效果:


    IMG_4677D226F011-1.jpeg

    代码如下:
    定义:

    @property (nonatomic,strong) UIImagePickerController *imagePicker;
    @property (nonatomic,strong) UIPopoverPresentationController* popover;
    

    因为iPad打开照片选择器必须通过UIPopoverPresentationController。

    在点击上传的按钮事件做以下操作:

    - (IBAction) uploadBtnAct:(id)sender{
        self.imagePicker = [[UIImagePickerController alloc] init];
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            self.imagePicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeMovie];
            self.imagePicker.navigationBar.translucent = NO;
            self.imagePicker.delegate = self;
            [self.imagePicker setAllowsEditing:NO];
            
            UIViewController *viewController = [[UIViewController alloc] init];
            UIView *pickerView = self.imagePicker.view;
            pickerView.translatesAutoresizingMaskIntoConstraints = NO;
            [viewController addChildViewController:self.imagePicker];
            [viewController.view addSubview:pickerView];
            viewController.modalPresentationStyle = UIModalPresentationPopover;
            
            UIButton *cameraButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [cameraButton setTitle:@"拍摄视频" forState:UIControlStateNormal];
            [cameraButton setTitleColor:UIColorFromRGB(0x007afc) forState:UIControlStateNormal];
            [cameraButton setFrame:CGRectMake(0, 200, CGRectGetWidth(viewController.view.frame), 44)];
            cameraButton.translatesAutoresizingMaskIntoConstraints = NO;
            [cameraButton addTarget:self action:@selector(handleCameraButtonEvent) forControlEvents:UIControlEventTouchUpInside];
            [viewController.view addSubview:cameraButton];
            
            [viewController.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[pickerView]-0-[cameraButton(44)]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(cameraButton,pickerView)]];
            [viewController.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[pickerView]-0-|" options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:nil views:NSDictionaryOfVariableBindings(pickerView)]];
            [viewController.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[cameraButton]-0-|" options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:nil views:NSDictionaryOfVariableBindings(cameraButton)]];
            
            _popover = viewController.popoverPresentationController;
            _popover.sourceView = sender;
            _popover.sourceRect = CGRectMake(0, MYUPLOAD_HEADER_OFFSET, sender.bounds.size.width, button.bounds.size.height);
            _popover.permittedArrowDirections = UIPopoverArrowDirectionUp;
            [self.fatherVC presentViewController:viewController animated:YES completion:nil];
        }
    }
    
    - (void)handleCameraButtonEvent {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == YES) {
            
            [self.imagePicker dismissViewControllerAnimated:YES completion:^{}];
            _popover = nil;
            self.imagePicker = [[UIImagePickerController alloc] init];
            self.imagePicker.navigationBar.translucent = NO;
            self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            self.imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
            self.imagePicker.mediaTypes = @[(NSString*)kUTTypeMovie];
            self.imagePicker.videoQuality = UIImagePickerControllerQualityTypeHigh;
            self.imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
            self.imagePicker.videoMaximumDuration = 30*60;
            self.imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
            self.imagePicker.showsCameraControls = YES;
            self.imagePicker.delegate = self;
            @weakify(self)
            [self.fatherVC presentViewController:self.imagePicker animated:YES completion:^{
                @strongify(self)
                [self setCameraAuthorizationStatus];
            }];
        }
    }
    

    相关文章

      网友评论

          本文标题:iPad图片上传的方法

          本文链接:https://www.haomeiwen.com/subject/mdgzictx.html