你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS從系統相冊選取多張照片示例代碼

iOS從系統相冊選取多張照片示例代碼

編輯:IOS開發綜合

之前寫過UIImagePickerController調取系統相冊圖片(選取單張照片,多用於用戶頭像)

1.pod導入

pod 'CTAssetsPickerController'

2.添加頭文件

#import <CTAssetsPickerController/CTAssetsPickerController.h>

3.添加delegate

<CTAssetsPickerControllerDelegate>

4.實現

- (void)upLoading {
  [PHPhotoLibraryrequestAuthorization:^(PHAuthorizationStatus status){
    if (status !=PHAuthorizationStatusAuthorized)return;
    dispatch_async(dispatch_get_main_queue(), ^{
      CTAssetsPickerController *picker = [[CTAssetsPickerControlleralloc] init];
      picker.delegate =self;
      // 顯示選擇的索引
      picker.showsSelectionIndex =YES;
      // 設置相冊的類型:相機膠卷 +自定義相冊
      picker.assetCollectionSubtypes =@[
                        @(PHAssetCollectionSubtypeSmartAlbumUserLibrary),
                        @(PHAssetCollectionSubtypeAlbumRegular)];
      // 不需要顯示空的相冊
      picker.showsEmptyAlbums =NO;
      [selfpresentViewController:pickeranimated:YEScompletion:nil];
    });
  }];
}

-(BOOL)assetsPickerController:(CTAssetsPickerController *)picker shouldSelectAsset:(PHAsset *)asset
{
  NSInteger max =9;
  if (picker.selectedAssets.count >= max) {
    UIAlertController *alert = [UIAlertControlleralertControllerWithTitle:@"提示"message:[NSStringstringWithFormat:@"最多選擇%zd張圖片", max] preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertActionactionWithTitle:@"好的"style:UIAlertActionStyleDefaulthandler:nil]];
    [picker presentViewController:alertanimated:YEScompletion:nil];
    // 這裡不能使用self來modal別的控制器,因為此時self.view不在Window上
    returnNO;
  }
  returnYES;
}

-(void)assetsPickerController:(CTAssetsPickerController *)picker didFinishPickingAssets:(NSArray *)assets
{
  
  NSArray *array =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
  NSString *documents = [arraylastObject];
  NSString *documentPath = [documentsstringByAppendingPathComponent:@"arrayXmlRss/ target=_blank class=infotextkey>Xml.XmlRss/ target=_blank class=infotextkey>Xml"];
  
  NSArray *dataArray = [NSArrayarrayWithArray:assets];
  
  
  [dataArray writeToFile:documentPathatomically:YES];
  
  
  
  NSArray *resultArray = [NSArrayarrayWithContentsOfFile:documentPath];
  NSLog(@"%@", documentPath);
  
  
  // 關閉圖片選擇界面
  [picker dismissViewControllerAnimated:YEScompletion:nil];
  
  // 遍歷選擇的所有圖片
  self.plcollection.photoArray = assets;
  for (NSInteger i =0; i < assets.count; i++) {
    // 基本配置
    CGFloat scale = [UIScreenmainScreen].scale;
    PHImageRequestOptions *options = [[PHImageRequestOptionsalloc] init];
    options.resizeMode  =PHImageRequestOptionsResizeModeExact;
    options.deliveryMode =PHImageRequestOptionsDeliveryModeHighQualityFormat;
    
    PHAsset *asset = assets[i];
    CGSize size =CGSizeMake(asset.pixelWidth / scale, asset.pixelHeight / scale);
//    // 獲取圖片
    [[PHImageManagerdefaultManager] requestImageForAsset:assettargetSize:size contentMode:PHImageContentModedefaultoptions:options resultHandler:^(UIImage *_Nullable result,NSDictionary *_Nullable info) {
      NSData *imageData =UIImageJPEGRepresentation([selfimageWithImageSimple:resultscaledToSize:CGSizeMake(200,200)], 0.5);
      [selfossUpload:imageData];

    }];
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。

[db:作者簡介][db:原文翻譯及解析]

【iOS從系統相冊選取多張照片示例代碼】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved