日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > windows >内容正文

windows

IOS开发调用系统相机和打开闪光灯

發(fā)布時間:2024/9/30 windows 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOS开发调用系统相机和打开闪光灯 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
IOS開發(fā)調(diào)用系統(tǒng)相機(jī)和打開閃光燈
?
? ?今天給大家分享一下如何調(diào)用iphone的拍照功能和打開閃光燈,有些代碼我也不太理解,很多是在網(wǎng)上借鑒其他人的。IOS有兩種的拍照和視頻的方式:1.直接使用UIImagePickerController,這個類提供了一個簡單便捷的拍照與選擇圖片庫里圖片的功能。2.另一種是通過AVFoundation.framework框架完全自定義拍照的界面和選擇圖片庫界面。我只做了第一種,就先給大家介紹第一種做法:
一、首先調(diào)用接口前,我們需要先判斷當(dāng)前設(shè)備是否支持UIImagePickerController,用isSourceTypeAvailable:來判斷是否可用
二、查看符合的媒體類型,這個時候我們調(diào)用availableMediaTypeForSourceType:判斷
在調(diào)用UIImagePickerController時我們需要加入他的兩個代理方法:
UINavigationControllerDelegate和UIImagePickerControllerDelegate,在調(diào)用攝像頭的時候還可以調(diào)閃光燈,一會代碼里有。

要調(diào)用閃光燈需要先建一個AVCaptureSession類的實例對象:

[java]?view plaincopy
  • #import?<UIKit/UIKit.h>??
  • //調(diào)用閃光燈調(diào)用框架??
  • #import?<AVFoundation/AVFoundation.h>??
  • ???
  • @interface?CameraViewController?:?UIViewController<UINavigationControllerDelegate,?UIImagePickerControllerDelegate>??
  • {??
  • ????AVCaptureSession?*?_AVSession;//調(diào)用閃光燈的時候創(chuàng)建的類??
  • }??
  • ???
  • @property(nonatomic,retain)AVCaptureSession?*?AVSession;??
  • ???
  • @end??
  • 在.m的- (void)viewDidLoad里建立4Button,Camera調(diào)用相機(jī)、Library調(diào)用圖片庫、flashlight打開閃光燈、close關(guān)閉閃光燈

    [java]?view plaincopy
  • //打開相機(jī)??
  • -(void)addCarema??
  • {??
  • ????//判斷是否可以打開相機(jī),模擬器此功能無法使用??
  • ????if?([UIImagePickerController?isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])?{??
  • ???????????
  • ????????UIImagePickerController?*?picker?=?[[UIImagePickerController?alloc]init];??
  • ????????picker.delegate?=?self;??
  • ????????picker.allowsEditing?=?YES;??//是否可編輯??
  • ????????//攝像頭??
  • ????????picker.sourceType?=?UIImagePickerControllerSourceTypeCamera;??
  • ????????[self?presentModalViewController:picker?animated:YES];??
  • ????????[picker?release];??
  • ????}else{??
  • ????????//如果沒有提示用戶??
  • ????????UIAlertView?*alert?=?[[UIAlertView?alloc]?initWithTitle:@"Error"?message:@"你沒有攝像頭"?delegate:nil?cancelButtonTitle:@"Drat!"?otherButtonTitles:nil];??
  • ????????[alert?show];??
  • ????}??
  • }??

  • 打開相機(jī)后,然后需要調(diào)用UIImagePickerControllerDelegate里的方法,拍攝完成后執(zhí)行的方法和點擊Cancel之后執(zhí)行的方法:

    [java]?view plaincopy
  • //拍攝完成后要執(zhí)行的方法??
  • -(void)imagePickerController:(UIImagePickerController?*)picker?didFinishPickingMediaWithInfo:(NSDictionary?*)info??
  • {??
  • ????//得到圖片??
  • ????UIImage?*?image?=?[info?objectForKey:UIImagePickerControllerOriginalImage];??
  • ????//圖片存入相冊??
  • ????UIImageWriteToSavedPhotosAlbum(image,?nil,?nil,?nil);??
  • ????[self?dismissModalViewControllerAnimated:YES];??
  • ???????
  • }??
  • //點擊Cancel按鈕后執(zhí)行方法??
  • -(void)imagePickerControllerDidCancel:(UIImagePickerController?*)picker??
  • {??
  • ????[self?dismissModalViewControllerAnimated:YES];??
  • }??

  • 調(diào)用相機(jī)照片和保存到圖片庫已經(jīng)完成。
    接著介紹打開照片庫:
    [java]?view plaincopy
  • -(void)openPicLibrary??
  • {??
  • ????//相冊是可以用模擬器打開的??
  • ????if?([UIImagePickerController?isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])?{??
  • ????????UIImagePickerController?*?picker?=?[[UIImagePickerController?alloc]init];??
  • ????????picker.delegate?=?self;??
  • ????????picker.allowsEditing?=?YES;//是否可以編輯??
  • ???
  • ????????//打開相冊選擇照片??
  • ????????picker.sourceType?=?UIImagePickerControllerSourceTypePhotoLibrary;??
  • ????????[self?presentModalViewController:picker??animated:YES];??
  • ????????[picker?release];??
  • ????}else{??
  • ????????UIAlertView?*alert?=?[[UIAlertView?alloc]?initWithTitle:@"Error"?message:@"你沒有攝像頭"?delegate:nil?cancelButtonTitle:@"Drat!"?otherButtonTitles:nil];??
  • ????????[alert?show];??
  • ????}??
  • ???????
  • }??
  • ???
  • //選中圖片進(jìn)入的代理方法??
  • -(void)imagePickerController:(UIImagePickerController?*)picker?didFinishPickingImage:(UIImage?*)image?editingInfo:(NSDictionary?*)editingInfo??
  • {??
  • ????[self?dismissModalViewControllerAnimated:YES];??
  • }??

  • 調(diào)用閃光燈的代碼

    [java]?view plaincopy
  • -(void)openFlashlight??
  • {??
  • ????AVCaptureDevice?*?device?=?[AVCaptureDevice?defaultDeviceWithMediaType:AVMediaTypeVideo];??
  • ????if?(device.torchMode?==?AVCaptureTorchModeOff)?{??
  • ????????//Create?an?AV?session??
  • ????????AVCaptureSession?*?session?=?[[AVCaptureSession?alloc]init];??
  • ???????????
  • ????????//?Create?device?input?and?add?to?current?session??
  • ????????AVCaptureDeviceInput?*?input?=?[AVCaptureDeviceInput?deviceInputWithDevice:device?error:nil];??
  • ????????[session?addInput:input];??
  • ???????????
  • ????????//?Create?video?output?and?add?to?current?session???
  • ????????AVCaptureVideoDataOutput?*?output?=?[[AVCaptureVideoDataOutput?alloc]init];??
  • ????????[session?addOutput:output];??
  • ???????????
  • ????????//?Start?session?configuration??
  • ????????[session?beginConfiguration];??
  • ????????[device?lockForConfiguration:nil];??
  • ???????????
  • ????????//?Set?torch?to?on??
  • ????????[device?setTorchMode:AVCaptureTorchModeOn];??
  • ???????????
  • ????????[device?unlockForConfiguration];??
  • ????????[session?commitConfiguration];??
  • ???????????
  • ????????//?Start?the?session??
  • ????????[session?startRunning];??
  • ???????????
  • ????????//?Keep?the?session?around??
  • ????????[self?setAVSession:self.AVSession];??
  • ???????????
  • ????????[output?release];??
  • ????}??
  • }??
  • ???
  • -(void)closeFlashlight??
  • {??
  • ????[self.AVSession?stopRunning];??
  • ????[self.AVSession?release];??
  • } ?
  • 總結(jié)

    以上是生活随笔為你收集整理的IOS开发调用系统相机和打开闪光灯的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。