在iOS開發(fā)中,照片和相冊圖片經(jīng)常被用作用戶的化身,有必要調(diào)用系統(tǒng)攝像頭或相冊以獲取圖片,下面就讓武林技術(shù)頻道小編帶你來看看iOS App中調(diào)用相冊中圖片及獲取最近的一張圖片的方法吧!
UIImagePickerController從拍照、圖庫、相冊獲取圖片
iOS 獲取圖片有三種方法:
1. 直接調(diào)用攝像頭拍照
2. 從相冊中選擇
3. 從圖庫中選擇
UIImagePickerController 是系統(tǒng)提供的用來獲取圖片和視頻的接口;
用UIImagePickerController 類來獲取圖片視頻,大體分為以下幾個步驟:
1. 初始化UIImagePickerController 類;
2. 設(shè)置UIImagePickerController 實例的數(shù)據(jù)來源類型(下面解釋);
3. 設(shè)置設(shè)置代理;
4. 如果需要做圖片修改的話設(shè)置allowsEditing =yes。
數(shù)據(jù)來源類型一共有三種:
?
enum {
?? UIImagePickerControllerSourceTypePhotoLibrary ,//來自圖庫
?? UIImagePickerControllerSourceTypeCamera ,//來自相機(jī)
?? UIImagePickerControllerSourceTypeSavedPhotosAlbum //來自相冊
};
在用這些來源的時候最好檢測以下設(shè)備是否支持;
?
?
?
?if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
??? {
??????? NSLog(@"支持相機(jī)");
??? }
??? if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
??? {
??????? NSLog(@"支持圖庫");
??? }
??? if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
??? {
??????? NSLog(@"支持相片庫");
??? }
調(diào)用攝像頭來獲取資源
?
?
?
- (void)viewDidLoad {
??? [super viewDidLoad];
??? picker = [[UIImagePickerController alloc]init];
??? picker.view.backgroundColor = [UIColor orangeColor];
??? UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypeCamera;
??? picker.sourceType = sourcheType;
??? picker.delegate = self;
??? picker.allowsEditing = YES;
}
上面只是實例了UIImagePickerController及其屬性 在需要獲取圖片的時候需要彈出窗口調(diào)用
?
?
?
[self presentViewController:picker animated:YES completion:nil];
我們還需要代理來獲取我們選中的圖片
?
UIImagePickerControllerDelegate
代理中一共三個方法 其中一個3.0 已經(jīng)廢棄了,只剩下兩個我們需要用的
?
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary
?*)info;
當(dāng)用戶選取完成后調(diào)用;
?
?
?
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
當(dāng)用戶取消選取時調(diào)用;
?
?
?
- (void)imagePickerController:(UIImagePickerController *)picker
?didFinishPickingMediaWithInfo:(NSDictionary *)info;
選取的信息都在info中,info 是一個字典。
?
字典中的鍵:
- NSString *const? UIImagePickerControllerMediaType ;指定用戶選擇的媒體類型(文章最后進(jìn)行擴(kuò)展)
- NSString *const? UIImagePickerControllerOriginalImage ;原始圖片
- NSString *const? UIImagePickerControllerEditedImage ;修改后的圖片
- NSString *const? UIImagePickerControllerCropRect ;裁剪尺寸
- NSString *const? UIImagePickerControllerMediaURL ;媒體的URL
- NSString *const? UIImagePickerControllerReferenceURL ;原件的URL
- NSString *const? UIImagePickerControllerMediaMetadata;當(dāng)來數(shù)據(jù)來源是照相機(jī)的時候這個值才有效
獲取最近的一張圖片
最近的需求需要我模仿微信聊天提示最新的一張圖片這個功能。
先講講思路。
思路很簡單,點擊“+”號的時候獲取相冊列表,獲取最新保存的一張圖片。根據(jù)圖片保存時間,與當(dāng)前時間戳進(jìn)行計算,獲得間隔時間。從而判斷是否是需求的時間間隔。(時間間隔自定義)
計算公式:當(dāng)前時間 - 圖片保存時間 <= 時間間隔
那么根據(jù)這個思路就開始寫一個分類。
它是關(guān)于ALAssetsLibrary的獲取功能,那么就根據(jù)他寫一個分類。
但是在iOS9后這個庫過時了,不過還是將他封裝一份,到時候新建一個工具類進(jìn)行適配就行了。
?
//ALAssetsLibrary+WJ.h
#import <AssetsLibrary/AssetsLibrary.h>
@interface ALAssetsLibrary (WJ)
/**
?*? 獲取最新一張圖片
?*
?*? @param block 回調(diào)
?*/
- (void)latestAsset:(void(^_Nullable)(ALAsset * _Nullable asset,NSError *_Nullable error)) block;
@end
//ALAssetsLibrary+WJ.m
#import "ALAssetsLibrary+WJ.h"
@implementation ALAssetsLibrary (WJ)
- (void)latestAsset:(void (^)(ALAsset * _Nullable, NSError *_Nullable))block {
??? [self enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
??????? if (group) {
??????????? [group setAssetsFilter:[ALAssetsFilter allPhotos]];
??????????? [group enumerateAssetsWithOptions:NSEnumerationReverse/*遍歷方式*/ usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
??????????????? if (result) {
??????????????????? if (block) {
??????????????????????? block(result,nil);
??????????????????? }
??????????????????? *stop = YES;
??????????????? }
??????????? }];
??????????? *stop = YES;
??????? }
??? } failureBlock:^(NSError *error) {
??????? if (error) {
??????????? if (block) {
??????????????? block(nil,error);
??????????? }
??????? }
??? }];
}
@end
在iOS8 PhotoKit已經(jīng)存在了。所以以后寫iOS8以上的應(yīng)用可以直接使用。
PhotoKit簡單實用封裝。具體詳細(xì)性能請看參考提供的鏈接。
?
?
?
//PHAsset+WJ.h
#import <Photos/Photos.h>
@interface PHAsset (WJ)
/**
?*? 獲取最新一張圖片
?*/
+ (PHAsset *)latestAsset;
@end
//PHAsset+WJ.m
#import "PHAsset+WJ.h"
@implementation PHAsset (WJ)
+ (PHAsset *)latestAsset {
??? // 獲取所有資源的集合,并按資源的創(chuàng)建時間排序
??? PHFetchOptions *options = [[PHFetchOptions alloc] init];
??? options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
??? PHFetchResult *assetsFetchResults = [PHAsset fetchAssetsWithOptions:options];
??? return [assetsFetchResults firstObject];
}
@end
根據(jù)需求本文章不提供其他功能寫法(ps:最主要本人也才剛使用>.<)。
圖片保存時間 都可以在對應(yīng)的asset(PHAsset,AlAsset)里面獲取到。
上面是iOS App中調(diào)用相冊中圖片及獲取最近的一張圖片的方法,相信大家都了解了,想要學(xué)習(xí)更多的技術(shù)知識,請收藏武林技術(shù)頻道!
?