菜鸟教程小白 发表于 2022-12-13 07:26:40

ios - 使用缓冲区 iOS 7 上传


                                            <p><p>我正在尝试使用随机数据实现上传并测量速度。现在我正在像这样生成我的随机 NSData:</p>

<pre><code>void * bytes = malloc(&#34;&#34;);
NSData * myData = ;
free(&#34;bytes&#34;);
</code></pre>

<p>但是如果我想上传一个大文件会出现内存问题...</p>

<p>我的上传过程是这样的:</p>

<pre><code>NSURLSessionConfiguration *sessionConfig =
;

NSURLSession *session =
[NSURLSession sessionWithConfiguration:sessionConfig
                              delegate:self
                         delegateQueue:nil];

NSURL * urll = ;
NSMutableURLRequest * urlRequest = ;
;
;

NSString *boundary = @&#34;*****&#34;;
NSString *contentType = ;
;

NSMutableData *body = ;
dataUsingEncoding:NSUTF8StringEncoding]];
];
dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
;

void * bytes = malloc(250000000);
NSData * uploadData = ;
free(bytes);

ulTask = ;

;
</code></pre>

<p>有没有办法用缓冲区或其他东西上传?!比如生成小数据,上传这个然后生成一个新的再上传?!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我建议开始上传并继续发送数据。您还可以避免创建 250mb 缓冲区,方法是使用 <code>uploadTaskWithStreamedRequest</code> 然后创建一个 <code>NSInputStream</code> 子类,该子类一直提供更多数据,直到您告诉它停止。您可以实现 <code>URLSession:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:</code> 来监控上传进度(因此您可以大概监控数据发送的速度)。</p>

<p>无论如何,创建上传请求:</p>

<pre><code>@interface ViewController () &lt;NSURLSessionDelegate, NSURLSessionTaskDelegate&gt;

@property (nonatomic, strong) CustomStream *inputStream;

@end

@implementation ViewController

- (void)viewDidLoad
{
    ;

    self.inputStream = [ init];

    NSURL *url = ;
    NSMutableURLRequest *request = ;
    ;

    NSURLSessionConfiguration *configuration = ;
    NSURLSession *session = ;

    NSURLSessionUploadTask *task = ;

    ;

    // I don&#39;t know how you want to finish the upload, but I&#39;m just going
    // to stop it after 10 seconds

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
      self.inputStream.finished = YES;
    });
}
</code></pre>

<p>您显然必须实现适当的委托(delegate)方法:</p>

<pre><code>#pragma mark - NSURLSessionTaskDelegate

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend
{
    NSLog(@&#34;%lld %lld %lld&#34;, bytesSent, totalBytesSent, totalBytesExpectedToSend);
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task needNewBodyStream:(void (^)(NSInputStream *bodyStream))completionHandler
{
    completionHandler(self.inputStream);
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
    NSLog(@&#34;%s: error = %@; data = %@&#34;, __PRETTY_FUNCTION__, error, [ initWithData:self.responseData encoding:NSUTF8StringEncoding]);
}

#pragma mark - NSURLSessionDataDelegate

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler
{
    self.responseData = ;
    completionHandler(NSURLSessionResponseAllow);
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
    ;
}
</code></pre>

<p>还有<code>CustomStream</code>:</p>

<pre><code>static NSInteger const kBufferSize = 32768;

@interface CustomStream : NSInputStream

@property (nonatomic, readonly) NSStreamStatus streamStatus;
@property (nonatomic, getter = isFinished) BOOL finished;

@end

@interface CustomStream ()

@property (nonatomic) NSStreamStatus streamStatus;
@property (nonatomic) void *buffer;

@end

@implementation CustomStream

- (instancetype)init
{
    self = ;
    if (self) {
      _buffer = malloc(kBufferSize);
      NSAssert(_buffer, @&#34;Unable to create buffer&#34;);
      memset(_buffer, 0, kBufferSize);
    }
    return self;
}

- (void)dealloc
{
    if (_buffer) {
      free(_buffer);
      self.buffer = NULL;
    }
}

- (void)open
{
    self.streamStatus = NSStreamStatusOpen;
}

- (void)close
{
    self.streamStatus = NSStreamStatusClosed;
}

- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)len
{
    if () {
      if (self.streamStatus == NSStreamStatusOpen) {
            self.streamStatus = NSStreamStatusAtEnd;
      }
      return 0;
    }

    NSUInteger bytesToCopy = MIN(len, kBufferSize);
    memcpy(buffer, _buffer, bytesToCopy);

    return bytesToCopy;
}

- (BOOL)getBuffer:(uint8_t **)buffer length:(NSUInteger *)len
{
    return NO;
}

- (BOOL)hasBytesAvailable
{
    return self.streamStatus == NSStreamStatusOpen;
}

- (void)scheduleInRunLoop:(__unused NSRunLoop *)aRunLoop
                  forMode:(__unused NSString *)mode
{}

- (void)removeFromRunLoop:(__unused NSRunLoop *)aRunLoop
                  forMode:(__unused NSString *)mode
{}

#pragma mark Undocumented CFReadStream Bridged Methods

- (void)_scheduleInCFRunLoop:(__unused CFRunLoopRef)aRunLoop
                     forMode:(__unused CFStringRef)aMode
{}

- (void)_unscheduleFromCFRunLoop:(__unused CFRunLoopRef)aRunLoop
                         forMode:(__unused CFStringRef)aMode
{}

- (BOOL)_setCFClientFlags:(__unused CFOptionFlags)inFlags
               callback:(__unused CFReadStreamClientCallBack)inCallback
                  context:(__unused CFStreamClientContext *)inContext {
    return NO;
}

@end
</code></pre>

<p>我建议你引用 BJ Homer 的文章 <a href="http://bjhomer.blogspot.com/2011/04/subclassing-nsinputstream.html" rel="noreferrer noopener nofollow">Subclassing NSInputStream</a>关于这个 <code>NSInputStream</code> 子类中一些神秘方法的一些背景知识。 </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 使用缓冲区 iOS 7 上传,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/22864565/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/22864565/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 使用缓冲区 iOS 7 上传