OGeek|极客世界-中国程序员成长平台

标题: ios - 对于 Ios,如何使用 objective-c 中的键将图像或视频发送到服务器 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 23:32
标题: ios - 对于 Ios,如何使用 objective-c 中的键将图像或视频发送到服务器

我是 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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4