iOS Development Multi-select photo implementation

Source: Internet
Author: User

#import "ViewController.h"#import<Photos/Photos.h>@interfaceViewcontroller () <uiimagepickercontrollerdelegate, uinavigationcontrollerdelegate>@property (Weak, nonatomic) Iboutlet Uiimageview*ImageView;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.}-(ibaction) selectimage {[Self getthumbnailimages];}/** * Get the original picture of all albums*/- (void) getoriginalimages{//get all your custom albumsPhfetchresult<phassetcollection *> *assetcollections =[phassetcollection fetchassetcollectionswithtype:phassetcollectiontypealbum subtype:    Phassetcollectionsubtypealbumregular Options:nil]; //Traverse all Custom Albums     for(Phassetcollection *assetcollectioninchassetcollections)    {[self enumerateassetsinassetcollection:assetcollection original:yes]; }        //get the camera rollPhassetcollection *cameraroll =[phassetcollection fetchassetcollectionswithtype:phassetcollectiontypesmartalbum subtype:    Phassetcollectionsubtypesmartalbumuserlibrary Options:nil].lastobject; [Self Enumerateassetsinassetcollection:cameraroll original:yes];}/** * Get thumbnail images from all albums*/- (void) getthumbnailimages{//get all your custom albumsPhfetchresult<phassetcollection *> *assetcollections =[phassetcollection fetchassetcollectionswithtype:phassetcollectiontypealbum subtype:    Phassetcollectionsubtypealbumregular Options:nil]; //Traverse all Custom Albums     for(Phassetcollection *assetcollectioninchassetcollections)    {[self enumerateassetsinassetcollection:assetcollection original:no]; }        //get the camera rollPhassetcollection *cameraroll =[phassetcollection fetchassetcollectionswithtype:phassetcollectiontypesmartalbum subtype:    Phassetcollectionsubtypesmartalbumuserlibrary Options:nil].lastobject; [Self Enumerateassetsinassetcollection:cameraroll original:no];}/** * traverse all pictures in the album * * @param assetcollection Albums * @param original if you want the original*/- (void) Enumerateassetsinassetcollection: (Phassetcollection *) assetcollection Original: (BOOL) original{NSLog (@"Photo Album name:%@", Assetcollection.localizedtitle); Phimagerequestoptions*options =[[Phimagerequestoptions alloc] init]; //sync to get pictures, only return 1 picturesOptions.synchronous =YES; //get all Phasset objects in a photo albumPhfetchresult<phasset *> *assets =[Phasset fetchassetsinassetcollection:assetcollection Options:nil];  for(Phasset *assetinchassets) {        //Do you want the originalCgsize size = original?Cgsizemake (Asset.pixelwidth, asset.pixelheight): Cgsizezero; //get pictures from asset[[Phimagemanager Defaultmanager] Requestimageforasset:asset targetsize:size contentmode:phimagecontentmodedefault Options:options resulthandler:^ (UIImage * _nullable result, nsdictionary *_nullable Info) {NSLog (@"%@", result);    }]; }}/** * Get all the pictures in the camera roll*/- (void) getimagesfromcameraroll{//get all the pictures in the camera rollPhfetchresult<phasset *> *assets =[Phasset Fetchassetswithoptions:nil]; __blockintCount =0;  for(Phasset *assetinchassets) {[[Phimagemanager Defaultmanager] Requestimageforasset:asset targetsize:cgsizemake (asset.pixelWidth, Asset.pixelHe ight) Contentmode:phimagecontentmodedefault Options:nil Resulthandler:^ (UIImage * _nullable result, nsdictionary *_nullable Info) {NSLog (@"%@-%zd", result, count++);    }]; }}/** * Use Uiimagepickercontroller to select pictures*/- (void) getimagefromipc{//Uiimagepickercontroller: You can get pictures from the app that comes with your system (Photos \ Camera)//determine if the album can be opened    if(! [Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypephotolibrary])return; Uiimagepickercontroller*IPC =[[Uiimagepickercontroller alloc] init]; //Open Photo app (show all albums)Ipc.sourcetype =uiimagepickercontrollersourcetypephotolibrary; //Open the Photos app (only show "moments" this album)//ipc.sourcetype = uiimagepickercontrollersourcetypesavedphotosalbum; //Camera//ipc.sourcetype = Uiimagepickercontrollersourcetypecamera;Ipc.Delegate=Self ; [Self PRESENTVIEWCONTROLLER:IPC animated:yes completion:nil];}#pragmaMark-<uiimagepickercontrollerdelegate>-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (NSDictionary<NSString *,ID> *) info{//destroying the controller[Picker dismissviewcontrolleranimated:yes Completion:nil]; //set up a pictureSelf.imageView.image =info[uiimagepickercontrolleroriginalimage];}@end

Summary: 1: Selecting Photos from an album can take advantage of uiimagepickercontroller, provided that you have to comply with two protocols:

<uiimagepickercontrollerdelegate, uinavigationcontrollerdelegate> But this method only gets one photo in the album. Use the following method: 1: You can first determine whether it supports the camera or album, does not support direct return, do not execute the following code 2: Create an object, set up an agent, set the sourcetype (note the difference between the two types), Pop-up with Presentviewcontroller can pop up the album, Dissmiss back to the app. Implement proxy method Get Picture: Self.imageView.image = Info[uiimagepickercontrolleroriginalimage];3:uiimagepickercontroller Display Chinese interface:

1.project-->info-->localizations adding Chinese

2. Modify Target-->info-->localization native development Region:china

-(void) GETIMAGEFROMIPC

{

Uiimagepickercontroller: You can get pictures from the app that comes with your system (Photos \ Camera)

Determine if the album can be opened

if (![ Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypephotolibrary]) return;

Uiimagepickercontroller *IPC = [[Uiimagepickercontroller alloc] init];

Open Photo app (show all albums)

Ipc.sourcetype = uiimagepickercontrollersourcetypephotolibrary;

Open the Photos app (only show "moments" this album)

Ipc.sourcetype = Uiimagepickercontrollersourcetypesavedphotosalbum;

Camera

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

{

Destroying the Controller

[Picker Dismissviewcontrolleranimated:yes Completion:nil];

Set up a picture

Self.imageView.image = Info[uiimagepickercontrolleroriginalimage];

}

2: If you want to choose more photos: 1: Available system framework: #import <Photos/Photos.h> 2: Use steps: 1: Get all albums first, get photo album Roll

Phassetcollection

2: Walk through the albums array, get each album photo Phasset, and then use Phasset request to get each picture

3: If you want to make the system album style: 1: This interface can be built using nine lattice, or the use of collection construction, the latter is recommended, because the latter can have a recycling mechanism, saving

Memory

2: The following interface to build: You can listen to each image of the click, when clicked on a picture, will be encapsulated mask and hook, cover it, or remove

iOS Development Multi-select photo implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.