Code Monkey home page Code Monkey logo

alassettonsinputstream's Introduction

#ALAssetToNSInputStream

When using NSURLRequest to upload a large file,we prefer to set HTTPBodyStream instead of HTTPBody in case of out of memory error if the file server support stream transfer.

If the large file is inside your sandbox,it would be easy to use initializer of NSURL.But if you choose a large video via ALAssetsLibrary and get a ALAsset instance,there is no direct way to convert ALAsset to NSInputStream.

Now you just need to add a single ALAssetToNSInputStream.swift to your project and you can easily get it through.

#Usage

  • Add ALAssetToNSInputStream.swift to your project
  • Get NSInputStream in either methods.
//assuming assetUrl is the `NSURL` of `ALAsset`
let inpurStream1 = NSInputStream.inputStreamWithAssetURL(assetUrl)
let inputStream2 = ALAssetNSInputStream(URL: assetUrl)

Note

inputStream1 is implemented by CFCreateBounderPair which use two stream to transfer data from one side to the other side,and will not change the behavior of NSInputStream.It's may be more safer.

inputStream2 is implemented by inheriting NSInputStream which completely overrides super methods,and changes the behavoir of NSInputStream.

  • Set HTTPBodyStream of NSMutableURLRequest to NSInputStream you got
  • Start your network request by NSURLConnection or NSURLSession

About Multipart Form Stream

If your server is designed for web application and only accept file using multipart/formdata,you can work around with PKMultipartInputStream.

let stream = PKMultipartInputStream()
let assetStream = ALAssetNSInputStream(URL: assetUrl)
let rept = assetStream.rept
stream.addPartWithName("fieldName", filename: rept.filename(), stream: assetStream, streamLength:UInt(rept.size()))

Then you got a NSInputStream containing ALAsset file with Content-Type of Multipart/formdata

Reference

iOS Developer Library

For large blocks of constructed data, call CFStreamCreateBoundPair to create a pair of streams, then call the setHTTPBodyStream: method to tell NSMutableURLRequest to use one of those streams as the source for its body content. By writing into the other stream, you can send the data a piece at a time.

ios-how-to-upload-a-large-asset-file-into-sever-by-streaming

alassettonsinputstream's People

Contributors

luowenxing avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.