ios 發(fā)開之相機(jī)、相冊

#import <MediaPlayer/MediaPlayer.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
@interface XXViewController ()
<
UIImagePickerControllerDelegate,
UINavigationControllerDelegate  //相機(jī)導(dǎo)航欄
>
@property(nonatomic, strong) UIImagePickerController *CamreaPicker;//相機(jī)
@property(nonatomic, strong) UIImagePickerController *PhotoPicker;//相冊
@property(nonatomic, strong) AVCaptureSession *AVSession;
@end
//啟動相冊
-(void)openPhoto{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"打開本地相冊" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self transferCameraAndPhoto:100];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"打開相機(jī)" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self transferCameraAndPhoto:200];
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}
-(void)transferCameraAndPhoto:(NSInteger)SkinType{ 
    switch (SkinType) {
        case 100:
            //判斷是否可以打開相機(jī)
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                _CamreaPicker = [[UIImagePickerController alloc]init];
                _CamreaPicker.delegate = self;
                _CamreaPicker.allowsEditing = YES;
                //攝像頭
                _CamreaPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                [self presentViewController:_CamreaPicker animated:YES completion:nil];
            }else{
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"對不起跋理,未檢測到攝像頭择克,無法進(jìn)行拍照" preferredStyle:UIAlertControllerStyleAlert];
                [alert addAction:[UIAlertAction actionWithTitle:@"確認(rèn)" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                    
                }]];
                [self presentViewController:alert animated:YES completion:nil];
            }
            break;
        case 200:
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
                _PhotoPicker = [[UIImagePickerController alloc]init];
                _PhotoPicker.delegate = self;
                _PhotoPicker.allowsEditing = YES;
                _PhotoPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                [self presentViewController:_PhotoPicker animated:YES completion:nil];
            }
            break;
            
        default:
            break;
    }
}

//開閃光燈

-(void)openFlashLight{
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    if (device.torchMode == AVCaptureTorchModeOff) {
        self.AVSession = [[AVCaptureSession alloc]init];
        AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
        [_AVSession addInput:input];
        
        AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc]init];
        [_AVSession addOutput:output];
        
        [_AVSession beginConfiguration];
        [device lockForConfiguration:nil];
        
        // Set torch to on
        [device setTorchMode:AVCaptureTorchModeOn];
        
        [device unlockForConfiguration];
        [_AVSession commitConfiguration];
        
        // Start the session
        [_AVSession startRunning];
        
        // Keep the session around
        [self setAVSession:self.AVSession];
    }
}
//關(guān)閃光燈

-(void)closeFlashLight{
    [self.AVSession stopRunning];
}
//相機(jī)回調(diào)函數(shù)
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    //獲取圖片名字
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    //獲取圖片的名字 
    if (image !=nil) {
        NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
        
        ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset){
            ALAssetRepresentation *representation = [myasset defaultRepresentation];
            NSString *photoName = [representation filename];
            
        };
        ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
        [assetslibrary assetForURL:imageURL resultBlock:resultblock failureBlock:nil];
        [picker dismissViewControllerAnimated:true completion:nil];
    }
    if (picker == _CamreaPicker) {
        //得到圖片
        //存入相冊
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
        [self dismissViewControllerAnimated:YES completion:nil];
    } 
}


PS:在info.plist中添加下面的,可以打開相機(jī)相冊權(quán)限
相機(jī): <key>NSCameraUsageDescription</key>
<string>cameraDesciption</string>

相冊: <key>NSPhotoLibraryUsageDescription</key>
<string>photoLibraryDesciption</string>

**實現(xiàn)調(diào)用相機(jī)前普,相冊的demo中借鑒了諸多前輩的開發(fā)博客肚邢,在此根據(jù)自己開發(fā)中遇到的問題統(tǒng)一整理成此篇,向前輩們表示感謝和致敬。 ``

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末骡湖,一起剝皮案震驚了整個濱河市贱纠,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌响蕴,老刑警劉巖谆焊,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異浦夷,居然都是意外死亡辖试,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進(jìn)店門劈狐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來罐孝,“玉大人,你說我怎么就攤上這事肥缔∩龅担” “怎么了?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵辫继,是天一觀的道長。 經(jīng)常有香客問我俗慈,道長姑宽,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任闺阱,我火速辦了婚禮炮车,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘酣溃。我一直安慰自己瘦穆,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布赊豌。 她就那樣靜靜地躺著扛或,像睡著了一般。 火紅的嫁衣襯著肌膚如雪碘饼。 梳的紋絲不亂的頭發(fā)上熙兔,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天,我揣著相機(jī)與錄音艾恼,去河邊找鬼住涉。 笑死,一個胖子當(dāng)著我的面吹牛钠绍,可吹牛的內(nèi)容都是我干的舆声。 我是一名探鬼主播,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼柳爽,長吁一口氣:“原來是場噩夢啊……” “哼媳握!你這毒婦竟也來了碱屁?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤毙芜,失蹤者是張志新(化名)和其女友劉穎忽媒,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體腋粥,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡晦雨,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了隘冲。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片闹瞧。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖展辞,靈堂內(nèi)的尸體忽然破棺而出奥邮,到底是詐尸還是另有隱情,我是刑警寧澤罗珍,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布洽腺,位于F島的核電站,受9級特大地震影響覆旱,放射性物質(zhì)發(fā)生泄漏蘸朋。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一扣唱、第九天 我趴在偏房一處隱蔽的房頂上張望藕坯。 院中可真熱鬧,春花似錦噪沙、人聲如沸炼彪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽辐马。三九已至,卻和暖如春局义,著一層夾襖步出監(jiān)牢的瞬間齐疙,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工旭咽, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留贞奋,地道東北人。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓穷绵,卻偏偏與公主長得像轿塔,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,864評論 2 354

推薦閱讀更多精彩內(nèi)容