91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

IOS打開照相機與本地相冊選擇圖片實例詳解

發布時間:2020-09-05 12:16:29 來源:腳本之家 閱讀:428 作者:HelloWord杰少 欄目:移動開發

IOS打開照相機與本地相冊選擇圖片

最近正好項目里面要集成“打開照相機與本地相冊選擇圖片”的功能,今天就在這邊給大家寫一個演示程序;打開相機拍攝后或者在相冊中選擇一張照片,然后將它顯示在界面上。好了廢話不多說,因為比較簡單直接上源碼。

首先,我們在頭文件中添加需要用到的actionSheet控件,顯示圖片的UIImageView控件,并且加上所需要的協議

#import <UIKit/UIKit.h> 
 
@interface ImagePickerViewController : UIViewController<UIImagePickerControllerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate> 
 
@property (strong, nonatomic) IBOutlet UIImageView *headImage; 
 
@property (strong, nonatomic) UIActionSheet *actionSheet; 
 
- (IBAction)clickPickImage:(id)sender; 
@end 

通過點擊我設置在界面中的按鈕來呼出actionSheet控件,來選擇相應的操作拍照或是在相冊中選擇相片,代碼如下:

// 
// ImagePickerViewController.m 
// testAuto 
// 
// Created by silicon on 15/5/9. 
// Copyright (c) 2015年 silicon. All rights reserved. 
// 
 
#import "ImagePickerViewController.h" 
 
@interface ImagePickerViewController () 
 
@end 
 
@implementation ImagePickerViewController 
 
@synthesize actionSheet = _actionSheet; 
 
- (void)viewDidLoad { 
  [super viewDidLoad]; 
  // Do any additional setup after loading the view from its nib. 
   
} 
 
- (void)didReceiveMemoryWarning { 
  [super didReceiveMemoryWarning]; 
  // Dispose of any resources that can be recreated. 
} 
 
 
/** 
 @ 調用ActionSheet 
 */ 
- (void)callActionSheetFunc{ 
  if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){ 
    self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"選擇圖像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"從相冊選擇", nil nil]; 
  }else{ 
    self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"選擇圖像" delegate:self cancelButtonTitle:@"取消"destructiveButtonTitle:nil otherButtonTitles:@"從相冊選擇", nil nil]; 
  } 
   
  self.actionSheet.tag = 1000; 
  [self.actionSheet showInView:self.view]; 
} 
 
// Called when a button is clicked. The view will be automatically dismissed after this call returns 
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ 
  if (actionSheet.tag == 1000) { 
    NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
    // 判斷是否支持相機 
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 
      switch (buttonIndex) { 
        case 0: 
          //來源:相機 
          sourceType = UIImagePickerControllerSourceTypeCamera; 
          break; 
        case 1: 
          //來源:相冊 
          sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
          break; 
        case 2: 
          return; 
      } 
    } 
    else { 
      if (buttonIndex == 2) { 
        return; 
      } else { 
        sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; 
      } 
    } 
    // 跳轉到相機或相冊頁面 
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; 
    imagePickerController.delegate = self; 
    imagePickerController.allowsEditing = YES; 
    imagePickerController.sourceType = sourceType; 
     
    [self presentViewController:imagePickerController animated:YES completion:^{ 
     
    }]; 
  } 
} 
 
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
  [picker dismissViewControllerAnimated:YES completion:^{ 
   
  }]; 
   
  UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
  self.headImage.image = image; 
} 
 
/* 
#pragma mark - Navigation 
 
// In a storyboard-based application, you will often want to do a little preparation before navigation 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
  // Get the new view controller using [segue destinationViewController]. 
  // Pass the selected object to the new view controller. 
} 
*/ 
 
- (IBAction)clickPickImage:(id)sender { 
   
  [self callActionSheetFunc]; 
} 
@end 

代碼比較簡單,也容易理解,運行的效果如下:

IOS打開照相機與本地相冊選擇圖片實例詳解

IOS打開照相機與本地相冊選擇圖片實例詳解

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

博白县| 霍林郭勒市| 楚雄市| 周口市| 密云县| 德令哈市| 安平县| 常熟市| 宁海县| 香河县| 延长县| 余江县| 原平市| 禄丰县| 葵青区| 长岭县| 马边| 梅河口市| 沁阳市| 宜州市| 娱乐| 温州市| 越西县| 德惠市| 平湖市| 垦利县| 巨野县| 永善县| 通江县| 沐川县| 天门市| 威信县| 安溪县| 正蓝旗| 陵川县| 比如县| 年辖:市辖区| 玛曲县| 邵阳市| 花莲县| 郸城县|