我有一种方法可以在UIWebview中加载一些youtube视频,我有很多视频,当我运行代码时,我收到收到内存警告,应用程序崩溃了。我尝试使用did received memory waring
修复它,但它无法正常使用这是我正在使用的方法: -
-(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);
}
我在这里有大约50个视频,所以我可以做些什么来避免这种情况我在 viewDidLoad 旁边调用这个方法。有没有办法更好地使用一个线程,有人可以帮助我吗?但我对ios上的线程没有任何了解。
答案 0 :(得分:0)
我无法为您编写完整的代码,但为您提供了一小段代码来帮助您。
你制作了一个scrollView,包含了要播放的视频的缩略图,并分别将这些网址保存在一个数组中
**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];
}
答案 1 :(得分:0)
看起来你想要使用UICollectionView
- 发生什么事情是你正在将大量的这些webview加载到内存中而且它太多了。您需要利用细胞重用。
阅读一些教程并让自己加快速度 -