• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 对于 Ios,如何使用 objective-c 中的键将图像或视频发送到服务器

[复制链接]
菜鸟教程小白 发表于 2022-12-12 23:32:16 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我是 ios 新手。 如何将图像或视频发送到 php 服务器。我有一个 key “图片” 用于发送图像和键“视频”发送视频文件。我需要用他们的 key 发送图像或视频,我该如何发送...?



Best Answer-推荐答案


使用 Post 方法。您应该将图像/视频存档到数据并制作表格数据以将其发送到服务器。 我建议您使用 Alamofire 来执行此操作。这是代码。

let imageData = UIPNGRepresentation(image)!

Alamofire.upload(imageData, to: "https://httpbin.org/post").responseJSON { response in
    debugPrint(response)
}

这里是 the link.

更新

如果您熟悉 OC,请改用 AFNetworking。这是代码。

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod"OST" URLString"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileURL:[NSURL fileURLWithPath"file://path/to/image.jpg"] name"file" fileName"filename.jpg" mimeType"image/jpeg" error:nil];
    } error:nil];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

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
                      [progressView setProgress:uploadProgress.fractionCompleted];
                  });
              }
              completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
                  if (error) {
                      NSLog(@"Error: %@", error);
                  } else {
                      NSLog(@"%@ %@", response, responseObject);
                  }
              }];

[uploadTask resume];

这里是 the link.

关于ios - 对于 Ios,如何使用 objective-c 中的键将图像或视频发送到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41391880/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap