iOS--圖像加載UIImagePickerController

一、圖像加載

iOS圖像加載通常有四種方式

1、相冊

用戶的相冊資源,UIImagePickerController讀取

2、應(yīng)用程序包

與應(yīng)用程序源文件同一目錄,可通過文件路徑讀取

//圖像路徑

NSString *path = [[NSBundle mainBundle] stringByAppendingPathComponent:@"icon.png"];

//通過路徑對應(yīng)的圖片文件

UIImage *image = [UIImage imageWithContentsOfFile:path];

3、沙盒

沙盒目錄既可以保存圖片,也可以通過路徑讀取

//沙盒下的Documents目錄

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/icon.png"];

//通過路徑對應(yīng)的圖片文件

UIImage *image = [UIImage imageWithContentsOfFile:path];

4、因特網(wǎng)

1)通過URL從網(wǎng)絡(luò)上下載圖像讀取

NSURL *url = [NSURL URLWithString:@"http://www.baidu.com/img/520.jpeg"];

2)NSData提供了簡單的網(wǎng)絡(luò)數(shù)據(jù)加載

NSData *data = [NSData dataWithContentsOfURL:url];

3)NSData轉(zhuǎn)UIImage

//網(wǎng)絡(luò)獲取數(shù)據(jù)

NSData *data = [NSData dataWithContentsOfURL:url];

//將NSData轉(zhuǎn)成UIImage

UIImage *image = [UIImage imageWithData:data];

4)UIImage轉(zhuǎn)NSData

//compressionQuality圖像的范圍為0.0(最低品質(zhì))到1.0(最高品質(zhì))的壓縮系數(shù)

NSData *data = UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality);

PS: 將UIImage對象轉(zhuǎn)話為NSData對象的兩種方式

1、UIImageJPEGRepresentation(UIImage *image,0.5)

最終得到的data對象數(shù)據(jù)量少,而且可以通過設(shè)置圖片質(zhì)量進(jìn)一步的減少數(shù)據(jù)量,雖然清晰度不如第二種方式,但對于大眾用戶來說區(qū)別不大

2、UIImagePNGRepresentation

最終得到的data隊(duì)形數(shù)據(jù)量大

二、將圖片保存到手機(jī)相冊

1、開始保存

UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);

2、保存結(jié)束以后調(diào)用該方法

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {

NSLog(@"%@",error);//如果為null表示已經(jīng)保存到相冊中

}

三、UIImagePickerController的基本介紹

1、相冊資源訪問通過UIImagePickerController來訪問

2、UIImagePickerController類繼承自UINavigationController,是個獨(dú)立的導(dǎo)航控制器,使用模態(tài)窗口的方式彈出。

四、UIImagePickerController的常用屬性和方法

1、拾取源類型,有三種類型

@property(nonatomic) UIImagePickerControllerSourceType sourceType

UIImagePickerControllerSourceTypePhotoLibrary:照片庫,默認(rèn)值

UIImagePickerControllerSourceTypeCamera:攝像頭

UIImagePickerControllerSourceTypeSavedPhotosAlbum:相薄

2、媒體類型? ? ?

@property(nonatomic,copy) NSArray *mediaTypes,需要導(dǎo)入框架<MobileCoreServices/MobileCoreServices.h>

默認(rèn)情況下此數(shù)組包含kUTTypeImage,所以拍照時可以不用設(shè)置;但是當(dāng)要錄像的時候必須設(shè)置,可以設(shè)置為kUTTypeVideo(視頻,但不帶聲音)或者kUTTypeMovie(視頻并帶有聲音)

3、視頻最大錄制時長,默認(rèn)為10s

@property(nonatomic) NSTimeInterval videoMaximumDuration

4、視頻質(zhì)量,枚舉類型

@property(nonatomic) UIImagePickerControllerQualityType? videoQuality

UIImagePickerControllerQualityTypeHigh:高清質(zhì)量

UIImagePickerControllerQualityTypeMedium:中等質(zhì)量,適合WiFi傳輸

UIImagePickerControllerQualityTypeLow:低質(zhì)量,適合蜂窩網(wǎng)傳輸

UIImagePickerControllerQualityType640x480:640*480

UIImagePickerControllerQualityTypeIFrame1280x720:1280*720

UIImagePickerControllerQualityTypeIFrame960x540:960*540

5、攝像頭設(shè)備,cameraDevice是枚舉類型

@property(nonatomic) UIImagePickerControllerCameraDevice? cameraDevice

UIImagePickerControllerCameraDeviceRear:前置攝像頭

UIImagePickerControllerCameraDeviceFront:后置攝像頭

6、設(shè)置圖像編輯,允許選取器框定和拉伸圖像。默認(rèn)為NO。

@property(nonatomic)BOOL? allowsEditing

7、UIImagePickerController的代理方法

1) 相冊圖片選中之后調(diào)用

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

//key:UIImagePickerControllerOriginalImage 取原始圖片

//key:UIImagePickerControllerEditedImage? 取編輯后的圖片

//key:UIImagePickerControllerMediaType? ? 獲取到媒體類型

UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];

//選中以后退出相冊

[picker dismissViewControllerAnimated:YES completion:NULL];

}

2) 取消按鈕的點(diǎn)擊事件

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

[picker dismissViewControllerAnimated:YES completion:NULL];

}

五、將視頻保存到相冊

1、判斷該視頻能否保存

UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(path)

1、若返回YES,開始保存,在這里傳入的是NSURL對象的path屬性

UISaveVideoAtPathToSavedPhotosAlbum(path, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);

2、保存以后調(diào)用

- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo: (void *)contextInfo, error為nil 代表保存成功

六、使用UIImagePickerController拍照和錄像

1、創(chuàng)建UIImagePickerController對象。

2、指定拾取源,平時選擇照片時使用的拾取源是照片庫或者相簿,可以指定攝像頭

3、設(shè)置媒體類型mediaType,注意如果是錄像必須設(shè)置,如果是拍照此步驟可以省略,因?yàn)閙ediaType默認(rèn)包含kUTTypeImage(注意媒體類型定義在MobileCoreServices.framework中)

4、展示UIImagePickerController(通常以模態(tài)窗口形式打開)。

5、拍照和錄制視頻結(jié)束后在代理方法中展示/保存照片或視頻

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

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

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