ios開(kāi)發(fā)--彈出相機(jī)相冊(cè)相關(guān)操作

13865632,1366,768.jpg

今天寫(xiě)道需要選取照片(可以拍照或者選取圖庫(kù)的),不廢話,上代碼
這個(gè)是點(diǎn)擊事件
<code>
//提交選項(xiàng)
-(void)sendDataAction:(UIButton *) button{

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"從相冊(cè)選擇", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view];

}
//這里做個(gè)判斷

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

}
//相機(jī)判斷

  • (void)toCameraPickingController
    {
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
    NSLog(@"Error:沒(méi)有照相設(shè)備");
    }
    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];這個(gè)是找的demo里面的,這個(gè)方法過(guò)時(shí)了,被下面的替代了
    [self presentViewController:cameraPicker animated:YES completion:nil];

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

    }
    }
    //圖庫(kù)判斷

  • (void)toPhotoPickingController
    {
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
    NSLog(@"Error:無(wú)圖片庫(kù)");
    }
    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];這個(gè)是找的demo里面的,這個(gè)方法過(guò)時(shí)了,被下面的替代了
    [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];這個(gè)是找的demo里面的,這個(gè)方法過(guò)時(shí)了,被下面的替代了
    [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];這個(gè)是找的demo里面的,這個(gè)方法過(guò)時(shí)了,被下面的替代了
    [self dismissViewControllerAnimated:YES completion:nil];
    }
    //設(shè)置一下屏幕
  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }

</code>

<code>
補(bǔ)充一下,攜帶參數(shù)上傳

  • (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];
    // 參數(shù)
    NSMutableDictionary *parameter = [NSMutableDictionary dictionary];
    parameter[@"token"] = "param....";
    // 訪問(wèn)路徑
    NSString *stringURL = [NSString stringWithFormat:@"%@%@",HOSTURL,kUploadAvatar];

    [manager POST:stringURL parameters:parameter constructingBodyWithBlock:^(id<AFMultipartFormData> 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(@"上傳錯(cuò)誤");
    }];
    }

</code>

13865647,1366,768.jpg
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容