gpt4 book ai didi

iphone - NSURLConnection 数据请求结束时没有数据

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:31:41 25 4
gpt4 key购买 nike

我正在使用 NSURLConnection 下载位图。我将委托(delegate)设置为 self,并实现方法 connection:didReceiveDataconnectionDidFinishLoading。收到数据后,我将其存储在 NSMutableData 对象(称为 input)中,然后在连接结束时,我使用它来创建位图。问题是我在 connection:didReceiveData 中收到数据,但在 connection:DidFinishLoading 中为空。我该如何解决这个问题?

- (void)start
{
NSURL *url = [NSURL URLWithString:src];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self
startImmediately:YES];
}

- (void)connection:(NSURLConnection *)conn
didReceiveData:(NSData *)d
{
NSLog(@"Data Received: %@", d);//This prints the correct data
//Add data chunk to input
[input appendData:d];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)conn
{
CPLog(@"Finished Receiving bitmap from server. Data received with length %d: %@", [input length], input);//this prints length 0 and a value of (null)
CFDataRef imgData = (__bridge CFDataRef) input;
CGDataProviderRef imgDataProvider = CGDataProviderCreateWithCFData (imgData);
CGImageRef image;
if (!imgDataProvider)
{
NSLog(@"Request Failed!");//happens every time
[callback handleBitmap:nil];
return;
}

image = CGImageCreateWithJPEGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault);


[callback handleBitmap:image];
return;

}

最佳答案

您应该为input 属性分配内存。通常在开始新的 url 连接之前完成:

- (void)start
{
self.input = [NSMutableData data];
// .... init and start url connection
}

关于iphone - NSURLConnection 数据请求结束时没有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12828802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com