如何在iPhone中存储来自服务器的视频/图像文件
我想通过服务器将视频/图像从一台iPhone发送到另一台iPhone。 我已使用HTTP POST成功地将文件发送到服务器。但问题是我想从服务器接收文件,并将其存储在该设备中作为进一步使用的数据 有人能给我一个示例代码来做同样的事情吗?你可以使用WiFi网络来做文件传输 用“你好”来做这件事。你可以从 另外,请参阅Apple.Developer示例 WiTap示例应用程序演示了如何实现应用程序之间的网络通信。使用Bonjour,该应用程序在本地网络上发布自己的广告,并在网络上显示该应用程序的其他实例的列表 实现连接建立并编写您自己的结构以传输数据,因为小数据块大小1024更好您可以使用WiFi网络进行文件传输 用“你好”来做这件事。你可以从 另外,请参阅Apple.Developer示例 WiTap示例应用程序演示了如何实现应用程序之间的网络通信。使用Bonjour,该应用程序在本地网络上发布自己的广告,并在网络上显示该应用程序的其他实例的列表如何在iPhone中存储来自服务器的视频/图像文件,iphone,xcode,ios5,Iphone,Xcode,Ios5,我想通过服务器将视频/图像从一台iPhone发送到另一台iPhone。 我已使用HTTP POST成功地将文件发送到服务器。但问题是我想从服务器接收文件,并将其存储在该设备中作为进一步使用的数据 有人能给我一个示例代码来做同样的事情吗?你可以使用WiFi网络来做文件传输 用“你好”来做这件事。你可以从 另外,请参阅Apple.Developer示例 WiTap示例应用程序演示了如何实现应用程序之间的网络通信。使用Bonjour,该应用程序在本地网络上发布自己的广告,并在网络上显示该应用程序的其他
实现连接建立并编写您自己的结构来传输数据,因为小数据块大小1024更好您可以成功地使用Post将数据发布到服务器。正当现在,您可以制作一个用于下载图像/视频的API。获取完数据后,将其放入应用程序的文档文件夹中。 关于API,为了简单起见,只需创建指向该文件的链接,然后使用NSURLConnection下载 创建要下载的URL连接:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:strFileUrl]]; //strFileURL is url of your video/image
NSURLConnection *conection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease];
[conec start];
[request release];
获取要保存数据的文件路径:
strFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:strFileName];
您的类必须采用NSURLConnectionLegate协议的3种方法:请阅读协议和委托
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// create
[[NSFileManager defaultManager] createFileAtPath:strFilePath contents:nil attributes:nil];
file = [[NSFileHandle fileHandleForUpdatingAtPath:strFilePath] retain];// read more about file handle
if (file) {
[file seekToEndOfFile];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)receivedata
{
//write each data received
if( receivedata != nil){
if (file) {
[file seekToEndOfFile];
}
[file writeData:receivedata];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
//close file after finish getting data;
[file closeFile];
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
//do something when downloading failed
}
如果要查看文件,请使用UIWebview加载:
NSURL *fileURL = [NSURL fileURLWithPath:strFilePath];
[wvReview loadRequest:[NSURLRequest requestWithURL:fileURL]];
您使用Post将数据成功发布到服务器。正当现在,您可以制作一个用于下载图像/视频的API。获取完数据后,将其放入应用程序的文档文件夹中。 关于API,为了简单起见,只需创建指向该文件的链接,然后使用NSURLConnection下载 创建要下载的URL连接:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:strFileUrl]]; //strFileURL is url of your video/image
NSURLConnection *conection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease];
[conec start];
[request release];
获取要保存数据的文件路径:
strFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:strFileName];
您的类必须采用NSURLConnectionLegate协议的3种方法:请阅读协议和委托
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// create
[[NSFileManager defaultManager] createFileAtPath:strFilePath contents:nil attributes:nil];
file = [[NSFileHandle fileHandleForUpdatingAtPath:strFilePath] retain];// read more about file handle
if (file) {
[file seekToEndOfFile];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)receivedata
{
//write each data received
if( receivedata != nil){
if (file) {
[file seekToEndOfFile];
}
[file writeData:receivedata];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
//close file after finish getting data;
[file closeFile];
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
//do something when downloading failed
}
如果要查看文件,请使用UIWebview加载:
NSURL *fileURL = [NSURL fileURLWithPath:strFilePath];
[wvReview loadRequest:[NSURLRequest requestWithURL:fileURL]];
拉姆沙德:谢谢。那么如何使用捆绑视频或PhotoGallery视频将视频文件存储为数据呢?拉姆沙德:谢谢。那么我如何使用捆绑视频或PhotoGallery视频将视频文件存储为数据呢?Quang Há:你能为这个提供一个示例代码吗?因为我是iPhone新手,我对这些东西不太熟悉:Quang Há:非常感谢。这正是我要找的Quang Há:你能为这个提供一个示例代码吗?因为我是iPhone新手,我对这些东西不太熟悉:广海:非常感谢。这正是我要找的