// 获得所有的自定义相簿PHFetchResult<PHAssetCollection *> *assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];// 遍历所有的自定义相簿for (PHAssetCollection *assetCollection in assetCollections) {}
Get a camera Roll photo album
// 获得相机胶卷PHAssetCollection *cameraRoll = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil].lastObject;
Get a thumbnail of a photo album
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];// 同步获得图片, 只会返回1张图片options.synchronous = YES;// 获得某个相簿中的所有PHAsset对象PHFetchResult<PHAsset *> *assets = [PHAsset fetchAssetsInAssetCollection:assetCollection options:nil];for (PHAsset *asset in assets) { CGSize size = CGSizeZero; // 从asset中获得图片 [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) { NSLog(@"%@", result); }];}
Get the original image of a photo album
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];// 同步获得图片, 只会返回1张图片options.synchronous = YES;// 获得某个相簿中的所有PHAsset对象PHFetchResult<PHAsset *> *assets = [PHAsset fetchAssetsInAssetCollection:assetCollection options:nil];for (PHAsset *asset in assets) { CGSize size = CGSizeMake(asset.pixelWidth, asset.pixelHeight); // 从asset中获得图片 [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) { NSLog(@"%@", result); }];}
Select pictures with Uiimagepickercontroller
// UIImagePickerController : 可以从系统自带的App(照片\相机)中获得图片// 判断相册是否可以打开if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return;UIImagePickerController *ipc = [[UIImagePickerController alloc] init];// 打开照片应用(显示所有相簿)ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;// 打开照片应用(只显示"时刻"这个相簿)// ipc.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;// 照相机// ipc.sourceType = UIImagePickerControllerSourceTypeCamera;ipc.delegate = self;[self presentViewController:ipc animated:YES completion:nil];#pragma mark - <UIImagePickerControllerDelegate>- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{ // 销毁控制器 [picker dismissViewControllerAnimated:YES completion:nil]; // 设置图片 self.imageView.image = info[UIImagePickerControllerOriginalImage];}
Nan Error
- Cause of error: 0 is counted as a divisor, such as 10/0
The simplest way to save a picture to a camera roll
UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);/** * 通过UIImageWriteToSavedPhotosAlbum函数写入图片完毕后就会调用这个方法 * * @param image 写入的图片 * @param error 错误信息 * @param contextInfo UIImageWriteToSavedPhotosAlbum函数的最后一个参数 */- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{ if (error) { [SVProgressHUD showErrorWithStatus:@"图片保存失败!"]; } else { [SVProgressHUD showSuccessWithStatus:@"图片保存成功!"]; }}
Save Pictures to a custom album
-(Ibaction) Save {/* phauthorizationstatusnotdetermined, the user has not made a selection phauthorizationstatusdenied, The user denies the current app access to the album (the user clicked "Do not allow") phauthorizationstatusauthorized the user allows the current app to access the album (the user clicked "OK") phauthorizationstatus Restricted, because parental control, resulting in the application cannot be a method album (with the user's choice is not related) *///Determine the authorization state phauthorizationstatus status = [Phphotolibrary au Thorizationstatus]; if (status = = phauthorizationstatusrestricted) {//Because Parental Controls cause the app to fail the method album (not related to the user's choice) [Svprogresshud Showerrorwithstat us:@ "Cannot access the album because of system reasons"]; } else if (status = = phauthorizationstatusdenied) {//user denies the current app access to the album (the user clicked "Do not Allow") Xmglog (@ "Remind users to go to [settings-privacy-photo-xxx] Open access open Off "); } else if (status = = phauthorizationstatusauthorized) {//user allows the current app to access the album (the user clicked "OK") [self saveimage]; } else if (status = = phauthorizationstatusnotdetermined) {//user has not made a selection//bullet box requesting user authorization [Phphotolibrary requesta uthorization:^ (phauthorizationstatus status) {if (Status = = Phauthorizationstatusauthorized) {//user clicked OK [self saveimage]; } }]; }}-(void) saveimage{//Phasset: A resource, such as a picture \ video//phassetcollection: A photo album//Phasset logo, which can be used to find the corresponding Phass Et object (Picture object) __block nsstring *assetlocalidentifier = nil; If you want to make changes to the album (edit), the code must be placed in the block of the Performchanges method [Phphotolibrary sharedphotolibrary] [phphotolibrary Sharedphotolibrary] performchanges:^{//1. Save picture A to "camera roll"//create picture request Assetlocalidentifier = [PHASSETCR Eationrequest Creationrequestforassetfromimage:self.imageview.image].placeholderforcreatedasset.localidentifier ; } completionhandler:^ (BOOL success, Nserror * _nullable error) {if (success = = NO) {[Self showerror:@] Save picture Failed! "]; Return }//2. Get album Phassetcollection *createdassetcollection = [self createdassetcollection]; if (createdassetcollection = = nil) {[Self showerror:@ ' Create album failed!]; Return } [[PhphotOlibrary Sharedphotolibrary] performchanges:^{//3. Add picture A In "Camera Roll" to "album" D//Get picture Phasset *asset = [Phasset fetchassetswithlocalidentifiers:@[assetlocalidentifier] options:nil].lastobject; Add picture to album request Phassetcollectionchangerequest *request = [Phassetcollectionchangerequest changerequestforassetc Ollection:createdassetcollection]; Add pictures to albums [Request Addassets:@[asset]]; } completionhandler:^ (BOOL success, Nserror * _nullable error) {if (success = = NO) {[Self show error:@ "Save picture Failed!";;} else {[Self showsuccess:@ ' save picture Successfully!];;}];}];} /** * Get album */-(phassetcollection *) createdassetcollection{//Find the corresponding album for this app from an existing album Phfetchresult<phassetcollectio n *> *assetcollections = [phassetcollection fetchassetcollectionswithtype:phassetcollectiontypealbum subtype: Phassetcollectionsubtypealbumregular Options:nil]; for (phassetcOllection *assetcollection in assetcollections) {if ([Assetcollection.localizedtitle Isequaltostring:xmgassetcolle Ctiontitle]) {return assetcollection; }}//did not find the corresponding album, you have to create a new album//error message Nserror *error = nil; Phassetcollection identification, which can be used to find the corresponding Phassetcollection object (album object) __block NSString *assetcollectionlocalidentifier = Nil [[Phphotolibrary Sharedphotolibrary] performchangesandwait:^{//Create album Request Assetcollectionlocalidentifier = [P Hassetcollectionchangerequest Creationrequestforassetcollectionwithtitle:xmgassetcollectiontitle]. Placeholderforcreatedassetcollection.localidentifier; } error:&error]; If there is an error message if (error) return nil; Get the album that you just created return [phassetcollection Fetchassetcollectionswithlocalidentifiers:@[assetcollectionlocalidentifier] Options:nil].lastobject;} -(void) Showsuccess: (NSString *) text{Dispatch_async (Dispatch_get_main_queue (), ^{[Svprogresshud showSuccessWit Hstatus:text];});} -(void) ShowError: (NSString *) text{Dispatch_async (Dispatch_get_main_queue (), ^{[Svprogresshud Showerrorwithsta Tus:text]; });}
Get all the albums you've customized