xcode 如何在 iPhone 中存储来自服务器的视频/图像文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12066719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:17:40  来源:igfitidea点击:

How to store a video/image file from a server in iPhone

iphonexcodeios5

提问by James

I want to send a video/image from one iPhone to another iPhone via a Server. I have successfully sent the file to the server using HTTP POST. But the problem is I want to receive the file from the server and store it in that device as data for further use.

我想通过服务器将视频/图像从一部 iPhone 发送到另一部 iPhone。我已成功使用 HTTP POST 将文件发送到服务器。但问题是我想从服务器接收文件并将其作为数据存储在该设备中以供进一步使用。

Can any one suggest me a sample code to do the same ?

任何人都可以建议我一个示例代码来做同样的事情吗?

回答by Quang Hà

You use Post to post data to server successfully. Right? Now you can make an API for downloading image/video. When you finish getting data, put it in document folder of your app. About API, for simplest, just create a link to that file, then use NSURLConnection to download.

您使用 Post 将数据成功发布到服务器。对?现在您可以制作一个用于下载图像/视频的 API。获取数据后,将其放入应用程序的文档文件夹中。关于 API,最简单的方法是创建一个指向该文件的链接,然后使用 NSURLConnection 进行下载。

Create a URL connection to download:

创建要下载的 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];

Get path of file to save data:

获取保存数据的文件路径:

strFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:strFileName];

Your class must adopt 3 methods of NSURLConnectionDelegate protocol: (please read about Protocol and Delegate)

你的类必须采用 NSURLConnectionDelegate 协议的 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
}

If you want to review you file, use a UIWebview to load it:

如果要查看文件,请使用 UIWebview 加载它:

NSURL *fileURL = [NSURL fileURLWithPath:strFilePath];
[wvReview loadRequest:[NSURLRequest requestWithURL:fileURL]];

回答by Shamsudheen TK

you can use WiFi networkto do the file transfer

您可以使用WiFi网络进行文件传输

Use Bonjourto do it. you can start from here

使用Bonjour来做到这一点。你可以从这里开始

Also please see the Apple.Developer sample WiTap

另请参阅 Apple.Developer 示例WiTap

The WiTap sample application demonstrates how to achieve network communication between applications. Using Bonjour, the application both advertises itself on the local network and displays a list of other instances of this application on the network.

WiTap 示例应用程序演示了如何实现应用程序之间的网络通信。使用 Bonjour,应用程序既可以在本地网络上宣传自己,又可以在网络上显示此应用程序的其他实例的列表。

Implement the connection establishment and write your own structure's to transfer you data as small chunk sizes (1024 is better)

实现连接建立并编写自己的结构以将数据传输为小块大小(1024 更好)