欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS从系统相册选取多张照片示例代码

程序员文章站 2024-02-15 19:48:16
之前写过uiimagepickercontroller调取系统相册图片(选取单张照片,多用于用户头像) 1.pod导入 pod 'ctassetspic...

之前写过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:@"arrayxml.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];

    }];
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。