I have a method that load some youtube videos in a UIWebview and i have lots of videos and when i am running the code i am getting Received memory warning and app get crashed. I tried to fix it using did received memory waring
but its not working this is the method that i am using :-
-(void) setScrollView:(UIScrollView *)scrollview :(NSArray *)contentArray{
int x=0;
for(NSDictionary *str in contentArray){
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(x, 0, 123, 123)];
webView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
webView.scrollView.scrollEnabled = NO;
webView.scrollView.bounces = NO;
NSString *link = [str objectForKey:@"UTUBEURL"];
link = [@"http://www.youtube.com/v/" stringByAppendingFormat:link];
NSLog(@"link = %@",link);
NSString *embedHTML = @"\
<html><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0 \">\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body width=\"%0.0f\" height=\"%0.0f\" style=\"margin:0\">\
<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\" autostart=\"true\"></embed>\
</body></html>";
NSString* html = [NSString stringWithFormat:embedHTML,webView.frame.size.width, webView.frame.size.height,link, webView.frame.size.width, webView.frame.size.height];
[webView loadHTMLString:html baseURL:nil];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(x,123,200,20)];
[title setFont:[UIFont systemFontOfSize:11]];
[title setBackgroundColor:[UIColor blackColor]];
[title setTextColor:[UIColor whiteColor]];
title.text = [NSString stringWithFormat:@" %@",[str objectForKey:@"DISPLAYTEXT"]];
x += webView.frame.size.width+2;
[scrollview addSubview:webView];
[scrollview addSubview:title];
}
scrollview.contentSize = CGSizeMake(x, scrollview.frame.size.height);
}
i am having about 50 videos here so what can i do to avoid this i am calling this method in side my viewDidLoad. Is there a way to do this better using a thread, can someone help me with this? but i dont have any knowledge on thread on ios.
I can not write the whole code for you, but giving you a short piece of code to help you.
You make a scrollView, having thumbnail images of what video to play and save the urls in an array respectively then
**In VideoPlayContoller.h**
#import <MediaPlayer/MediaPlayer.h>
@interface VideoPlayViewController : UIViewController{
MPMoviePlayerController *moviePlayerController;
}
@property (nonatomic, readwrite) int i;
@property (nonatomic, retain) NSMutableArray *allVideoUrls;
-(void)playVideo:(int)_index;
- (IBAction)onTapBack:(id)sender;
@end
**In VideoPlayContoller.m**
#import "VideoPlayViewController.h"
#import <MediaPlayer/MediaPlayer.h>
@implementation VideoPlayViewController
@synthesize i,allVideoUrls;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
allVideoUrls = [[NSMutableArray alloc]init];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self playVideo:i];
}
-(void)playVideo:(int)_index{
NSURL *fileURL = [NSURL URLWithString:[allVideoUrls objectAtIndex:i]];
if(!moviePlayerController){
moviePlayerController = [[MPMoviePlayerController alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlaybackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doneButtonClick:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
}
moviePlayerController.fullscreen = YES;
[moviePlayerController setContentURL:fileURL];
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0){
[moviePlayerController.view setFrame:CGRectMake(0, 62, 320, 504)];
}
else{
[moviePlayerController.view setFrame:CGRectMake(0, 62, 320, 416)]; //y = 42
}
[self.view addSubview:moviePlayerController.view];
[moviePlayerController play];
}
- (IBAction)onTapBack:(id)sender {
[moviePlayerController stop];
[self dismissModalViewControllerAnimated:NO];
}
- (void)moviePlaybackComplete:(NSNotification *)notification{
i++;
[moviePlayerController stop];
[moviePlayerController.view removeFromSuperview];
[self dismissModalViewControllerAnimated:NO];
[self dismissModalViewControllerAnimated:NO];
}
-(void)doneButtonClick:(NSNotification*)aNotification{
NSLog(@"Done button Clicked");
[self dismissModalViewControllerAnimated:NO];
}