【发布时间】:2014-01-09 00:59:30
【问题描述】:
我有一种方法可以在 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 上的线程一无所知。
【问题讨论】:
-
分配大量的uiwebview是相当昂贵的
-
为什么要创建 50 个 Webview??,创建一个 WebView 并替换仅在该 WebView 上播放的 url,它与您的要求不同吗?
-
您可以使用滑动手势来更改要在该 Webview 上播放的视频,并根据它替换您的 UILabel。
-
@mAc 是的,它与我的请求不同,我想在我的主屏幕上添加一组视频,当用户按下播放按钮时,视频应该像视频库一样播放
-
创建您自己的播放器控件,但不是 web 视图
标签: ios iphone ios5 uiwebview didreceivememorywarning