iphone - How to generate images from already recorded video using AVAssetImageGeneratorCompletionHandler block? -
this might silly question sorry newbie iphone.
i want generate images every frames of video , display in uiscrollview inside uiimageview.
when trying getting memory warning , app crashes.
below code.
- (ibaction)oncamerabuttonpressed:(id)sender { // initialize uiimagepickercontroller select movie camera roll. uiimagepickercontroller *videopicker = [[uiimagepickercontroller alloc] init]; videopicker.delegate = self; videopicker.modalpresentationstyle = uimodalpresentationcurrentcontext; videopicker.sourcetype = uiimagepickercontrollersourcetypesavedphotosalbum; videopicker.mediatypes = @[(nsstring*)kuttypemovie]; [self presentviewcontroller:videopicker animated:yes completion:nil]; } #pragma mark image picker controller delegate - (void)imagepickercontroller:(uiimagepickercontroller *)picker didfinishpickingmediawithinfo:(nsdictionary *)info { [self dismissviewcontrolleranimated:yes completion:nil]; currentvideourl = info[uiimagepickercontrollerreferenceurl]; //video url = assets-library://asset/asset.mov?id=d0a4a3ee-c5f3-49c8-9e45-adf775b9fa8c&ext=mov //nslog(@"video url = %@",currentvideourl); //imageindex = 0; (uiimageview *subview in thumbnailscrollview.subviews) { [subview removefromsuperview]; } [self generatethumbnailsfromvideourl:currentvideourl]; //[self generateavassetthumbnails:currentvideourl]; //[self appleimagegenerator:currentvideourl]; } - (void)generatethumbnailsfromvideourl:(nsurl *)videourl { avurlasset *asset=[[avurlasset alloc] initwithurl:videourl options:nil]; //nslog(@"video duration %lld seconds",asset.duration.value/asset.duration.timescale); int videoduration = (asset.duration.value/asset.duration.timescale); if (cmtimearray.count>0) { [cmtimearray removeallobjects]; } (int = 0; i<videoduration; i++) { int64_t tempint = i; cmtime tempcmtime = cmtimemake(tempint,1); int32_t interval = 15; (int j = 1; j<16; j++) { cmtime newcmtime = cmtimemake(j,interval); cmtime addition = cmtimeadd(tempcmtime, newcmtime); [cmtimearray addobject:[nsvalue valuewithcmtime:addition]]; } } //nslog(@"array of time %@ count = %d",cmtimearray, cmtimearray.count); /*for(int t=0;t < asset.duration.value;t++) { cmtime thumbtime = cmtimemake(t,asset.duration.timescale); nsvalue *v=[nsvalue valuewithcmtime:thumbtime]; [cmtimearray addobject:v]; } nslog(@"array of time %@ count = %d",cmtimearray, cmtimearray.count);*/ __block int = 0; //__block uiimage *currentimage = nil; avassetimagegeneratorcompletionhandler handler = ^(cmtime requestedtime, cgimageref im, cmtime actualtime, avassetimagegeneratorresult result, nserror *error){ if (result == avassetimagegeneratorsucceeded) { // currentimage = [uiimage imagewithcgimage:im]; //[framesarray addobject:[uiimage imagewithcgimage:im]]; [self performselectoronmainthread:@selector(insertimagetoscrollview:) withobject:[uiimage imagewithcgimage:im] waituntildone:no]; } else nslog(@"ouch: %@", error.description); i++; imageindex = i; if(i == cmtimearray.count) { //[self performselectoronmainthread:@selector(insertimagetoscrollview:) withobject:framesarray waituntildone:yes]; } }; // launching process... self.generator = [[avassetimagegenerator alloc] initwithasset:asset]; self.generator.appliespreferredtracktransform=true; self.generator.requestedtimetolerancebefore = kcmtimezero; self.generator.requestedtimetoleranceafter = kcmtimezero; self.generator.maximumsize = cgsizemake(40, 40); [self.generator generatecgimagesasynchronouslyfortimes:cmtimearray completionhandler:handler]; } -(void)insertimagetoscrollview:(uiimage *)image { int xposition = (5*imageindex)+(width*imageindex)+offset; uiimageview *currentimageview = [[uiimageview alloc]initwithframe:cgrectmake(xposition, 5, width, width)]; currentimageview.tag = imageindex+10; currentimageview.image = image; [thumbnailscrollview addsubview:currentimageview]; [thumbnailscrollview setcontentsize:cgsizemake(xposition+width+5,thumbnailscrollview.frame.size.height)]; }
you loading images view, few displayed @ 1 given time. depending on amount of images, take lot of memory.
you consider following approach:
generate images , write them local storage, store file-urls of images in array.
now scrollview, add images needed, when scroll next ones. when adding images, load them disk , add them view. take @ example code horizontal infinite scrollview: https://code.google.com/p/iphone-infinite-horizontal-scroller.
you use ordinary uitableview custom cell, showing image.
Comments
Post a Comment