ios - 对于 Ios,如何使用 objective-c 中的键将图像或视频发送到服务器
<p><p>我是 ios 新手。
如何将图像或视频发送到 php 服务器。我有一个 key <strong>“图片”</strong>
用于<strong>发送图像</strong>和键<strong>“视频”</strong>发送视频文件。我需要用他们的 key 发送图像或视频,我该如何发送...?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>使用 <code>Post</code> 方法。您应该将图像/视频存档到数据并制作表格数据以将其发送到服务器。
我建议您使用 Alamofire 来执行此操作。这是代码。</p>
<pre><code>let imageData = UIPNGRepresentation(image)!
Alamofire.upload(imageData, to: "https://httpbin.org/post").responseJSON { response in
debugPrint(response)
}
</code></pre>
<p>这里是 <a href="https://github.com/Alamofire/Alamofire" rel="noreferrer noopener nofollow">the link.</a> </p>
<p><strong>更新</strong> </p>
<p>如果您熟悉 OC,请改用 AFNetworking。这是代码。</p>
<pre><code>NSMutableURLRequest *request = [ multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil];
} error:nil];
AFURLSessionManager *manager = [ initWithSessionConfiguration:];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
uploadTaskWithStreamedRequest:request
progress:^(NSProgress * _Nonnull uploadProgress) {
// This is not called back on the main queue.
// You are responsible for dispatching to the main queue for UI updates
dispatch_async(dispatch_get_main_queue(), ^{
//Update the progress view
;
});
}
completionHandler:^(NSURLResponse * _Nonnull response, id_Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"%@ %@", response, responseObject);
}
}];
;
</code></pre>
<p>这里是 <a href="https://github.com/AFNetworking/AFNetworking" rel="noreferrer noopener nofollow">the link.</a> </p></p>
<p style="font-size: 20px;">关于ios - 对于 Ios,如何使用 objective-c中的键将图像或视频发送到服务器,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/41391880/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/41391880/
</a>
</p>
页:
[1]