gpt4 book ai didi

使用 NSURLConnection 的 Iphone JPEG 数据流不包含图像

转载 作者:行者123 更新时间:2023-12-03 20:24:19 25 4
gpt4 key购买 nike

我之前是通过使用 dataWithContentsOfURL 下载 jpg 然后 writeToFile 来保存它来为我的应用程序下载图像。

我最近开始使用 NSURLConnetion 来执行相同的操作,但现在我收到以下错误和崩溃:

损坏的 JPEG 数据:87 个无关字节JPEG 数据流不包含图像

我知道这些图像没有损坏,因为应用程序使用以前的方法可以很好地下载它们。这是我的代码:

-(void) downloadSave {

NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

NSString *tempString = [[NSString alloc]initWithFormat:@"http://www.mysite.com/%@.jpg",chartFileName];
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:tempString]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
// create the connection with the request
// and start loading the data

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
mutableData = [[NSMutableData data] retain];
self.image = nil;
NSLog(@"connection exists");
[NSURLConnection connectionWithRequest:theRequest delegate:self];

} else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Connection Error" message:@"There was an error contacting the chart servers. Please try again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
[activityIndicator stopAnimating];


}




// NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
// NSUserDomainMask, YES);

// NSString *docsPath = [paths objectAtIndex:0];
// NSString *downloadPath = [[[NSString alloc]initWithFormat:@"http://www.mysite.com/%@.jpg",chartFileName]autorelease];
// downloadedChartData = nil;


[pool drain];
[pool release];




}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// This method is called when the server has determined that it
// has enough information to create the NSURLResponse.

// It can be called multiple times, for example in the case of a
// redirect, so each time we reset the data.

// receivedData is an instance variable declared elsewhere.
NSLog(@"got to connection did receive response");
[mutableData setLength:0];
}




- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
[mutableData appendData:data];
// NSLog(@"got some data, total: %i",mutableData.length);
}


- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
// release the connection, and the data object
// [connection release];
// receivedData is declared as a method instance elsewhere
// self.mutableData = nil;

// inform the user
//NSLog(@"Connection failed! Error - %@ %@",
// [error localizedDescription],
// [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// do something with the data
// receivedData is declared as a method instance elsewhere
NSLog(@"Succeeded! Received %d bytes of data",[mutableData length]);
[connection release];
// release the connection, and the data object
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
self.image = nil;

NSString *savePath = [[[NSString alloc]initWithFormat:@"%@/%@.jpg",docsPath, chartFileName]autorelease];

[mutableData writeToFile:savePath atomically:YES];
self.mutableData = nil;

最佳答案

您正在使用同一个委托(delegate)初始化并启动两个 NSURLConnections。由于您的委托(delegate)方法不检查调用它们的连接,因此您在一个 NSMutableData 实例中混合了两倍图像的字节。

// Creates, initializes and starts an instance of NSURLConnection    
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
...
// Creates, initializes and starts another instance of NSURLConnection, with same request and delegate
[NSURLConnection connectionWithRequest:theRequest delegate:self];

两个连接在同一个委托(delegate)实例上向相同的实现发送消息,这意味着它们的数据以随机顺序写入相同的 NSMutableData 中。

我建议简单地去掉这条线:

[NSURLConnection connectionWithRequest:theRequest delegate:self];

另一件事:为什么在 downloadSave 中使用自动释放池?如果您从主线程调用它,则该池中只有一个自动释放的 NSURLRequest。如果您从其他线程调用它,则必须注意该线程的运行循环已设置并正在运行,否则您根本不会收到任何委托(delegate)回调。

关于使用 NSURLConnection 的 Iphone JPEG 数据流不包含图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3145956/

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