iOS 拍照(相機)/相冊獲取圖片

iOS 從相機/相冊獲取圖片的文章很多,本文也并沒有對系統(tǒng)的相冊/相機界面做多少優(yōu)化.只是從程序健壯性,用戶體驗的角度對我以往的代碼進(jìn)行封裝優(yōu)化

首先,demo地址:https://github.com/sun6762/getPicture.git

準(zhǔn)備工作
a. 要能使真機在使用時成功調(diào)用相冊/拍照功能必須在info.plist,添加
Privacy - Camera Usage Description & Privacy - Photo Library Usage Description,當(dāng)然,在測試的時候根據(jù)提示添加更好

b. 要使用imgPickerController的代理必須有UIImagePickerControllerDelegate, UINavigationControllerDelegate2個代理

@interface ViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate> 
@end

用戶開啟相機權(quán)限

 // 引入以下2個類
#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>

AVAuthorizationStatus avStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];

//  avStatus 的4中類型
/*
AVAuthorizationStatusNotDetermined  // 初次調(diào)用
AVAuthorizationStatusRestricted //  禁用
AVAuthorizationStatusDenied // 
AVAuthorizationStatusAuthorized // 開通權(quán)限
*/

// 用戶開放相機權(quán)限后 判斷相機是否可用
BOOL useable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];

用戶開啟相冊權(quán)限,跟上述過程相似

// 導(dǎo)入此類
#import <Photos/Photos.h>

PHAuthorizationStatus phStatus = [PHPhotoLibrary authorizationStatus];
// 同樣 phStatus 有4中類型
/*
     PHAuthorizationStatusNotDetermined = 0
     PHAuthorizationStatusRestricted
     PHAuthorizationStatusDenied
     PHAuthorizationStatusAuthorized
     */

// 判斷相冊權(quán)限
[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]

打開相機/相冊

// 打開相機/相冊
- (void)openSuccWith:(NSString *)flag{
    
    UIImagePickerController *photoPicker = [UIImagePickerController new];
    photoPicker.delegate = self;
    photoPicker.allowsEditing = NO;
    
    if ([flag isEqualToString:AV]) {
        photoPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        
    }
    if ([flag isEqualToString:PH]) {
        photoPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    
    [self presentViewController:photoPicker animated:YES completion:nil];
}

用戶關(guān)閉iOS拍照/相冊權(quán)限,引導(dǎo)用戶打開拍照/相冊權(quán)限

- (void)guideUserOpenAuth{
    
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"溫馨提示" message:@"請打開訪問權(quán)限" preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *alertA = [UIAlertAction actionWithTitle:@"確定" style:(UIAlertActionStyleDefault) handler:nil];
    UIAlertAction *act = [UIAlertAction actionWithTitle:@"去設(shè)置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 引導(dǎo)用戶設(shè)置
        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            
            [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
        }
    }];
    [alertC addAction:alertA];
    [alertC addAction:act];
    [self presentViewController:alertC animated:YES completion:nil];
}

UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
    
    if ([mediaType isEqualToString:@"public.image"]) {
        UIImage * image;
        // 判斷,圖片是否允許修改
        if ([picker allowsEditing]){
            //獲取用戶編輯之后的圖像
            image = [info objectForKey:UIImagePickerControllerEditedImage];
        } else {
            // 照片的元數(shù)據(jù)參數(shù)
            image = [info objectForKey:UIImagePickerControllerOriginalImage];
        }
        
        // 壓縮圖片
        UIImage *compressImg = [self compressPictureWith:image];
        self.imgView.image = compressImg;
        //        NSLog(@"%@",NSStringFromCGSize(compressImg.size));
        // 用于上傳
        NSData *tmpData = UIImageJPEGRepresentation(compressImg, 0.5);
        
    }
    [self dismissViewControllerAnimated:YES completion:NULL];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:nil];
}

我們獲取的圖片上傳到服務(wù)器,都需要圖片壓縮,這也有,是不是很貼心

/ 壓縮圖片
- (UIImage *)compressPictureWith:(UIImage *)originnalImage{
    CGFloat ruleWidth = 600;
    if (originnalImage.size.width < ruleWidth) {
        return originnalImage;
    }
    
    CGFloat hight = ruleWidth/originnalImage.size.width * originnalImage.size.height;
    CGRect rect = CGRectMake(0, 0, ruleWidth, hight);
    // 開啟圖片上下文
    UIGraphicsBeginImageContext(rect.size);
    // 將圖片渲染到圖片上下文
    [originnalImage drawInRect:rect];
    // 獲取圖片
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    // 關(guān)閉圖片上下文
    UIGraphicsEndImageContext();
    return img;
}

....
以后有時間再將打開界面優(yōu)化一下

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

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

  • 因為要結(jié)局swift3.0中引用snapKit的問題,看到一篇介紹Xcode8,swift3變化的文章,覺得很詳細(xì)...
    uniapp閱讀 4,852評論 0 12
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,725評論 25 709
  • APP開發(fā)避免不開系統(tǒng)權(quán)限的問題,今天做定位時需要在不允許定位的時候做一些操作,所以,今天就大概的了解了一些。 權(quán)...
    SunshineBrother閱讀 16,915評論 4 62
  • 2016年的夏天,一個夜晚,我坐在房間里工作,窗外傳來對面屋子里的吵架聲,中文的,普通話的,吵架聲。是一對母女在爭...
    全世界都想上我閱讀 258評論 0 0
  • 微信網(wǎng)頁版即使用 Stylish 或 Stylebot 配置也很難看,所以直接用 wine 虛擬機。wine 上裝...
    Waste_Land閱讀 7,877評論 6 1

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