Add the hidden titleLabel to the parameter is the best solution.
I generated a array arrUrl which store the NSURL of the mov files in my phone album by enumerate assets block.
After that, I grab on Frame, lets say, get the frame at 3:00 second in the movie file, and generated the image file from the frame.
Next, loop over the arrUrl, and use program generate the button with image in the button, append the button to subview of the self.view.
Because I have to pass the movie Url to playMovie function, I have to assign the button.titleLabel.text with one movie url. and the the button events function, retrieve the url from the buttontitleLable.txt.
-(void)listVideos
{
for(index=0;index<[self.arrUrl count];index++{
UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
imageButton.frame = CGRectMake(20,50+60*index,50,50);
NSURL *dUrl = [self.arrUrl objectAtIndex:index];
[imageButton setImage:[[UIImage allow] initWithCGImage:*[self getFrameFromeVideo:dUrl]] forState:UIControlStateNormal];
[imageButton addTarget:self action:@selector(playMovie:) forControlEvents:UIControlEventTouchUpInside];
imageButton.titleLabel.text = [NSString strinfWithFormat:@"%@",dUrl];
imageButton.titleLabel.hidden = YES;
[self.view addSubView:imageButton];
}
}
-(void)playMovie:(id) sender{
UIButton *btn = (UIButton *)sender;
NSURL *movUrl = [NSURL URLWithString:btn.titleLabel.text];
moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movUrl];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
}
-(CGIImageRef *)getFrameFromVideo:(NSURL *)mUrl{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:mUrl option:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform = YES;
NSError *error =nil;
CMTime = CMTimeMake(3,1);
CGImageRef imageRef = [generator copyCGImageAtTime:time actualTime:nil error:&error];
if(error !=nil) {
NSLog(@"%@",sekf,error);
}
return @imageRef;
}