ios - Optimizing an array of UIImages -
so i'm building app user takes pictures of themselves, saves them camera roll, , i'm saving references asset urls display them in app. @ first model seemed work fine, took more , more pictures started receiving memory warnings , crashed. there better way approach this?
this how load saved photos @ launch of app (which freezes app 10 seconds depending on how many being loaded):
- (void) loadphotosarray { _photos = [[nsmutablearray alloc] init]; nsdata* data = [[nsuserdefaults standarduserdefaults] objectforkey: @"savedimages"]; if (data) { nsarray* storedurls = [[nsarray alloc] initwitharray: [nskeyedunarchiver unarchiveobjectwithdata: data]]; // reverse array nsarray* urls = [[storedurls reverseobjectenumerator] allobjects]; (nsurl* asseturl in urls) { // block handle image handling success alassetslibraryassetforurlresultblock resultblock = ^(alasset *myasset) { alassetrepresentation *rep = [myasset defaultrepresentation]; cgimageref iref = [rep fullresolutionimage]; if (iref) { uiimage* tempimage = [uiimage imagewithcgimage:iref]; uiimage* image = [[uiimage alloc] initwithcgimage: tempimage.cgimage scale: 1.0 orientation: uiimageorientationright]; // set image in imageview [_photos addobject: image]; [[nsnotificationcenter defaultcenter] postnotificationname: @"photoschanged" object: self]; } }; // handles failure of getting image alassetslibraryaccessfailureblock failureblock = ^(nserror *myerror) { nslog(@"can't image - %@",[myerror localizeddescription]); }; // load image call appropriate block alassetslibrary* assetslibrary = [[alassetslibrary alloc] init]; [assetslibrary assetforurl: asseturl resultblock: resultblock failureblock: failureblock]; } } else { nslog(@"photo storage empty"); } }
and saving photos:
- (void) addimagetophotos: (uiimage*)image { // store image @ front of array nsmutablearray* temp = [[nsmutablearray alloc] initwithobjects: image, nil]; // load rest of images onto temp array (uiimage* image in _photos) { [temp addobject: image]; } _photos = nil; _photos = [[nsmutablearray alloc] initwitharray: temp]; // [self.photos addobject: image]; [[nsnotificationcenter defaultcenter] postnotificationname: @"photoschanged" object: self.photos]; // save cache alassetslibrary* library = [[alassetslibrary alloc] init]; [library saveimage: image toalbum: @kalbumename withcompletionblock:^(nserror *error) { if (error) { nslog(@"error saving"); } }]; }
i think have 2 methods optimize problem.
u should save image name string instead of saving uiimage object, when need display image, use pagination display image according saved image name string.
u should use multi-thread deal long time task, recommend u use gcd load image name string.
Comments
Post a Comment