-1

I need to do the following:

  1. Convert the UIImage into a byte array;
  2. Get a string using multipart and write that string into a text file;
  3. Call API where that text file along with the remaining service parameters (request) have to upload it to the server.

I tried but I can't get the multipart string which should be written into the text file.

I am a beginner, if anyone can suggest me an approach to get the multipart image string I would be highly thankful.

Thanks in advance!

Dmitry Gamolin
  • 934
  • 1
  • 12
  • 29

1 Answers1

0

Use this below can write data to file: I have print the path of write path in the console, check the writed file by finder -> goto finder, and pay attention to if your image url is HTTP prefix, if yes, please set App Transport Security Settings in your Info.plist.

In objective-c:

NSURLSession *session = [NSURLSession sharedSession];
NSURL *url = [NSURL URLWithString:@"https://sf-sponsor.b0.upaiyun.com/45751d8fcd71e4a16c218e0daa265704.png"];  // if http:please set `App Transport Security Settings` in your Info.plist

NSURLSessionTask *task = [session dataTaskWithURL:url
                                completionHandler:^(NSData *data, NSURLResponse *response, NSError* error) {


                                    NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);

                                    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true);
                                    NSString *documentsDirectory = paths[0];
                                    NSLog(@"%@", documentsDirectory);
                                    NSFileManager * fm = [NSFileManager defaultManager];

                                    BOOL ifsucess = [fm createFileAtPath:[NSString stringWithFormat:@"%@/file.data", documentsDirectory] contents:data attributes:nil];

                                    if (ifsucess) {

                                        // success
                                    }else {

                                        // fail
                                    }



                                }];

[task resume];

In swift:

    /* swift */

    let path:String = "https://sf-sponsor.b0.upaiyun.com/45751d8fcd71e4a16c218e0daa265704.png"  // if http:please set `App Transport Security Settings` in your Info.plist
    let url:NSURL = NSURL.init(string: path)!
    let request = URLRequest.init(url: url as URL)

    let task = URLSession.shared.dataTask(with: request as URLRequest){ data, response, error in

        let paths:NSArray = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) as NSArray
        let documentsDirectory = paths.object(at: 0)
        print(documentsDirectory)
        //let appFile = "\(documentsDirectory)/MyFile.text"  //

        if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {

            let path = dir.appendingPathComponent("file.data")

            do {
                try data?.write(to: path, options: NSData.WritingOptions.atomic)
            }
            catch {/* error handling here */}

        }

    }

    task.resume()
aircraft
  • 25,146
  • 28
  • 91
  • 166
  • You are downloading an image from a remote URL and saving it as .data file in documents dir? Please add your rationale as to why should it work for the OPs clearly different requirements and why shouldn't the OP use multipart post request instead? – NSNoob Nov 22 '16 at 07:04