简体   繁体   English

如何在iOS中将NSdata转换为字符串

[英]How to convert NSdata to string in ios

I'm trying to view the video in tableview from my server so fetching the video from the server using the json in ios. 我正在尝试从服务器的tableview查看视频,因此使用ios中的json从服务器获取视频。 I have successfully connected the url to nsurl connection and appending to the nsdata everything is fine but the problem is when converting the nsdata to string it through the null. 我已经成功地将url连接到nsurl连接,并且将nsdata附加到一切都很好,但是问题是当将nsdata转换为通过null的字符串时。

this is code I have used for the url connection: 这是我用于url连接的代码:

-(void)setDataSource:(vedios *)inVideosObj
{
    //here I'm connecting the nsobject to outlets of the tablecell so here have successfully connected to url connection video is nsobject

    self.titile.text = inVideosObj.title;

    url =[NSURL URLWithString:inVideosObj.video];
    NSURLRequest *request =[NSURLRequest requestWithURL:url];

    NSLog(@"UDSA %@  %@ ",url,request);

    connection =[[NSURLConnection alloc] initWithRequest:request delegate:self];
    self.responseData = [[NSMutableData alloc]init];
}

after that I have checked that rul request passing in console I'm getting the url like this: 之后,我检查了在控制台中传递的rul请求,我得到的网址是这样的:

2014-01-25 13:00:35.587 video[1835:a0b] UDSA http://localhost/image/vedios/reddy.mp4  <NSURLRequest: 0xb992c40> { URL: http://localhost/image/vedios/reddy.mp4 } 

after I'm appending to the nsdata this is the code: 在附加到nsdata之后,这是代码:

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

     //NSLog(@"dad %@",self.responseData);
}

once again I printed the nsdata which I have append to in my console im getting like this: 再次打印出我已添加到控制台的nsdata,如下所示:

  2014-01-25 14:07:42.024 video[1911:a0b] dad <0000001c 66747970 6d703432 00000000    69736f6d 61766331 6d703432 00008d64 6d6f6f76 0000006c 6d766864 00000000 c8a43177 c8a43177 00000258 00010b78 00010000 01000000 00000000 00000000 00010000 00000000 00000000 00000000 00010000 00000000 00000000 00000000 40000000 00000000 00000000 00000000 00000000 00000000 00000000 00000003 00000015 696f6473 00000000 1007004f ffff2915 ff000052 19747261 6b000000 5c746b68 64000000 01c8a431 77c8a431 77000000 01000000 0000010b 6d000000 00000000 00000000 00010000 00000100 00000000 00000000 00000000 

now here I'm trying to convert the nsdata to string and passing the movieplayer 现在在这里,我试图将nsdata转换为字符串并传递movieplayer

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *urlString = [[NSString alloc]initWithData:self.responseData encoding:NSASCIIStringEncoding];

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

    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];
}

after converting video not playing the tablevewcell I now printed the converted the nsdata sring it's giving the null in my console 转换视频后不播放tablevewcell,现在我将转换后的nsdata打印出来,在控制台中显示为null

2014-01-25 14:12:18.622 video[1934:a0b] data :

I don't no what to do im stuck here for very long I have everything to convert the nsdata to string its showing the same null 我很长时间都没有卡住的东西,我拥有将nsdata转换为显示相同null的字符串的所有功能

this are the link I have tied for the solution research links1 link2 这是我为解决方案研究而绑定的链接links1 link2

nothing is working please help me how to resolve this problem where I'm doing wrong 没有任何效果,请帮助我在做错事情时如何解决此问题

this is the output for the self.responsedata 这是self.responsedata的输出

014-01-25 14:24:08.151 video[2019:a0b] reponse=<0000001c 66747970 6d703432 00000000 69736f6d 61766331 6d703432 00008d64 6d6f6f76 0000006c 6d766864 00000000 c8a43177 c8a43177 00000258 00010b78 00010000 01000000 00000000 00000000 00010000 00000000 00000000 00000000 00010000 00000000 00000000 00000000 40000000 00000000 00000000 00000000 00000000 00000000 00000000 00000003 00000015 696f6473 00000000 1007004f ffff2915 ff000052 19747261 6b000000 5c746b68 64000000 01c8a431 77c8a431 77000000 01000000 0000010b 6d000000 00000000 00000000 00010000 00000100 00000000 00000000 00000000 00000100 00000000 00000000 00000000 00400000 00000000 

It is very strange thing you do here 您在这里做的事很奇怪

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

   MPMoviePlayerController *mov = [[MPMoviePlayerController alloc]initWithContentURL: [NSURL URLWithString:urlString]];

In your self.responseData you've got NSData of required mp4 file. 在您的self.responseData中,您已获得所需mp4文件的NSData。 You only need to store it 您只需要存储它

[self.responseData writeToFile:pathToFile atomically:NO];

in Documents directory as it is. 在“文档”目录中保持原样。 Then you should make local NSURL with path of file pathToFile 然后,您应该使用文件pathToFile的路径创建本地NSURL

NSURL *localURL = [NSURL fileURLWithPath:pathToFile];

and use it 并使用它

MPMoviePlayerController *mov = [[MPMoviePlayerController alloc]initWithContentURL:localURL];

What kind of data is present in inVideosObj.video? inVideosObj.video中存在哪些数据?

If it is url for the video, the you should pass this url directly in MPMoviePlayerController . 如果它是视频的网址,则您应该直接在MPMoviePlayerController传递此网址。

MPMoviePlayerController *mov = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:inVideosObj.video]];

If it contains the video data, then you need to write that data in a file and give file path in MPMoviePlayerController . 如果其中包含视频数据,则需要将该数据写入文件,并在MPMoviePlayerController提供文件路径。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"video.mp4"];

[inVideosObj.video writeToFile:path atomically:YES];

NSURL *videoUrl= [NSURL fileURLWithPath:path];

MPMoviePlayerController *mov = [[MPMoviePlayerController alloc] initWithContentURL: videoUrl];

This this, if your data is not null terminated, 如果您的数据不是以null终止的,这就是这个,

NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

If your data is null terminated, try this: 如果您的数据以null结尾,请尝试以下操作:

NSString *str = [NSString stringWithUTF8String:[data bytes]];

Edit 编辑

Check this , it may help. 对此进行检查,可能会有所帮助。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM