如何在ios中将NSdata转换为nsurl

时间:2014-01-24 08:48:16

标签: ios nsdata

我尝试使用URL在tableView中播放视频我现在使用json从数据库中获取URL我使用NSMutabledata从数据库中获取字符串我附加了NSData。

当我传递数据时,我转换为字符串,并且我将URL传递给电影播放器 问题是我在我的控制台中得到空错误任何人都可以建议我如何将NSData转换为URL我长期困在这里需要帮助

这个代码我用来从json获取字符串并将其传递给url

-(void)setDataSource:(vedios *)inVideosObj
{
    self.titile.text = inVideosObj.title;

    url =[NSURL URLWithString:inVideosObj.video];
    NSURLRequest *request =[NSURLRequest requestWithURL:url];
    connection =[[NSURLConnection alloc] initWithRequest:request delegate:self];
    self.responseData = [[NSMutableData alloc]init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
{
     [self.responseData appendData:data];
}

这是我试图将nsdata转换为字符串并传递给电影播放器​​的代码。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // NSAssert(self.responseData, @"Response data should not be nil");

    NSString *urlString = [[NSString alloc]initWithData:self.responseData encoding:NSUTF8StringEncoding];

    // NSString *urlString = [NSString stringWithUTF8String:[self.responseData bytes] ];

    NSAssert(self.responseData, @"This should not be nil for a valid response");

    NSLog(@"data :%@",urlString);
    MPMoviePlayerController *mov = [[MPMoviePlayerController alloc]initWithContentURL: [NSURL URLWithString:urlString]];
    //  MPMoviePlayerController *mov = [[MPMoviePlayerController   alloc]initWithContentURL:[NSURL URLWithString:urlString]];
    NSLog(@"length %d",[self.responseData length]);

    // MPMoviePlayerController *mov = [[MPMoviePlayerController   alloc]initWithContentURL:url];

    self.movieplayer = mov;
    [self.movieplayer.view setFrame:CGRectMake(10,20, 100, 100)];

    // self.movieplayer.movieSourceType = MPMovieSourceTypeFile;
    [self.contentView addSubview:self.movieplayer.view];
    [self.movieplayer prepareToPlay];
    [self.movieplayer play];
}

0 个答案:

没有答案