ios - How to compare PHAsset to UIImage -


i have converted phasset uiimage :

 phimagemanager *manager = [phimagemanager defaultmanager];             [manager requestimageforasset:asset                                targetsize:phimagemanagermaximumsize                               contentmode:phimagecontentmodedefault                                   options:requestoptions                             resulthandler:^void(uiimage *image, nsdictionary *info) {                                 convertedimage = image;                                 [images addobject:convertedimage];                             }]; 

now want that:

[selectedassets removeobject:image]; 

where selectedassets array of phasset , image uiimage

so have implemented isequal that:

- (bool)isequal:(id)other {     if (other == self)         return yes;     if (!other || ![[other class] isequal:[self class]])         return no;      nsdata *data1 = uiimagepngrepresentation(self.image);     nsdata *data2 = uiimagepngrepresentation(((tinselectedimage*)other).image);      return   [data1 isequal:data2]; } 

but not work !

you should not compare images, should instead compare phassets or useful part named localidentifier.

the thing looking distinguish between assets called localidentifier property of phasset.

the apple docs define as:

a unique string persistently identifies object. (read-only) 

sorry answer bit broad because don't approach here.

if you, i'd this:

first create custom class, let's name photoinfo. (you don't have if not interested in keeping lot of info photos. if that's case, use pffetchresults of phassets directly if want to. go customclass).

in photoinfo.h:

#import <foundation/foundation.h> @interface photoinfo : nsobject  @property nsstring *localidentifier;  @end   

now instead of using array of images, use custom class created contain localidentifier. this:

photoinfo *photo = [[photoinfo alloc] init]; photo.localidentifier = asset.localidentifier; 

let's want fetch images gallery, this:

-(phfetchresult*) getassetsfromlibrary {     phfetchresult *allphotos;     phfetchoptions *allphotosoptions = [[phfetchoptions alloc] init];     allphotosoptions.sortdescriptors = @[[nssortdescriptor sortdescriptorwithkey:@"creationdate" ascending:no]]; //get images sorted creation date      allphotos = [phasset fetchassetswithmediatype:phassetmediatypeimage options:allphotosoptions];      return allphotos; } 

to populate datasource, like:

nsmutablearray *yourphotodatasource = [[nsmutablearray alloc] init]; phfetchresult * assets = [self getassetsfromlibrary];  for(phasset *asset in assets)         {             photoinfo *photo = [photoinfo new];             photo.localindentifier = asset.localidentifier;             [yourphotodatasource addobject:photo];          } 

now let's have display images somewhere , need actual image image:

-(void) getimageforasset: (phasset *) asset andtargetsize: (cgsize) targetsize andsuccessblock:(void (^)(uiimage * photoobj))successblock {     dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{         phimagerequestoptions *requestoptions;          requestoptions = [[phimagerequestoptions alloc] init];         requestoptions.resizemode   = phimagerequestoptionsresizemodefast;         requestoptions.deliverymode = phimagerequestoptionsdeliverymodefastformat;         requestoptions.synchronous = true;         phimagemanager *manager = [phimagemanager defaultmanager];         [manager requestimageforasset:asset                            targetsize:targetsize                           contentmode:phimagecontentmodedefault                               options:requestoptions                         resulthandler:^void(uiimage *image, nsdictionary *info) {                             @autoreleasepool {                                  if(image!=nil){                                     successblock(image);                                 }                             }                         }];     });  } 

now let's displaying images in tableview, in cellforrowatindexpath, call above mentioned method this:

  //show spinner   // give customizable size image. why load memory full image if don't need show it?  [self getimageforasset:asset andtargetsize:yourdesiredcgsizeofimage andsuccessblock:^(uiimage *photoobj) {             dispatch_async(dispatch_get_main_queue(), ^{                 //update ui of cell                 //hide spinner                 cell.thumbnail.image = photoobj;             });         }]; 

now loading images asynchronously smooth user experience , saving memory showing images needed instead of storing of them. (you can make performance better introducing caching that's not point here).

finally getting question, remove image need localidentifier because unique every phasset or index.

let's assume deleting cell @ tableview showing specific image want remove.

- (void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath {  if (editingstyle == uitableviewcelleditingstyledelete) {     photoinfo *photo = [yourphotodatasource objectatindex:indexpath.row];     [yourphotodatasource removeobject:photo];     [tableview deleterowsatindexpaths:[nsarray arraywithobject:indexpath]                      withrowanimation:uitableviewrowanimationfade];   } } 

if not using tableview/collectionview , not know index of object, can use fast enumeration on array must know localidentifier of object wanna delete:

-(void) deletephotowithidentifier:(nsstring *) identifierstr{ nsmutablearray *dummyarray = [[nsmutablearray alloc] initwitharray:yourphotodatasource]; //created because can't modify array iterating on. otherwise crashes.  [dummyarray enumerateobjectswithoptions:nsenumerationreverse usingblock:^(photoinfo *p, nsuinteger index, bool *stop) {     if ([p.localindentifier isequaltostring:idenfierstr]) {         [yourphotodatasource removeobjectatindex:index];     } }];  } 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -