NSURLSession and stream upload in background

后端 未结 4 1660
遇见更好的自我
遇见更好的自我 2021-01-02 10:19

I have some problems with using NSURLSession to upload photos from Asset Library to the server.

At first NSURLSession doesn\'t support stre

相关标签:
4条回答
  • 2021-01-02 10:50

    A clean workaround will be to create a NSOperation which will copy the file from the asset library to your temporary folder using NSStream, so you won't get a crash in case of a huge file, when the operation completes you schedule a upload of that temporary file, when the upload finishes you delete it.

    In my case, i need to send the file in multipart format so the creation of the temporary file is necessary but i encounter a problem in uploading large files, more then 2 Gb, example movies over 20 minutes.

    0 讨论(0)
  • 2021-01-02 10:55

    You can't upload NSData in background you need to upload file format. You can create it by directory path

    0 讨论(0)
  • 2021-01-02 10:58

    Convert to NSData and copy and write in app folder

    ALAsset *asset = [cameraRollUploadImages objectAtIndex:startCount];
    ALAssetRepresentation *representation = [asset defaultRepresentation];
    
    // create a buffer to hold the data for the asset's image
    uint8_t *buffer = (Byte *)malloc(representation.size);// copy the data from the asset into the buffer
    NSUInteger length = [representation getBytes:buffer 
                                      fromOffset:0 
                                          length:representation.size 
                                           error:nil];
    
    // convert the buffer into a NSData object, free the buffer after
    NSData *image = [[NSData alloc] initWithBytesNoCopy:buffer 
                                                 length:representation.size
                                           freeWhenDone:YES];
    
    0 讨论(0)
  • 2021-01-02 11:05

    Right now, there is no way otherthan saving the picture to local file system or temp directory.

    Following code make sure your data won't be lost with exif tags. (ALAsset => NSData)

    ALAssetRepresentation *assetRepresentation = [(ALAsset *)assetBeingUploaded defaultRepresentation];
    uint8_t *buffer = (uint8_t *)malloc(sizeof(uint8_t)*[assetRepresentation size]);
    NSUInteger buffered = 0;
    if (buffer != NULL)
    buffered = [assetRepresentation getBytes:buffer fromOffset:0.0 length:assetRepresentation.size error:nil];
    self.imageBeingUploaded = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
    

    The upload task in background session doesn't support completion handler. We should go for.,

    - (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL;  
    

    I doubt how do we get the response headers or body in case if we are using background session & uploadtask with request using file?

    0 讨论(0)
提交回复
热议问题