문제

다음과 같이 nsurlconnection을 사용하여 HTTP보다 이미지를 얻고 있습니다.

NSMutableData *receivedData;

- (void)getImage {
    self.receivedData = [[NSMutableData alloc] init];
    NSURLConnection *theConnection = // create connection
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {    
   [receivedData appendData:data];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
   [connection release];

   UIImage *theImage = [UIImage imageWithData:receivedData];
}

일반적으로 잘 작동하지만 때로는 이것이 로그인되는 것을보고 있습니다 - : 손상된 JPEG 데이터 : 조기 데이터 세그먼트의 조기 끝

이 시점에서 이미지가 완전히 렌더링되지 않습니다. 아마도 75%를 볼 수 있고 오른쪽 하단 코너는 회색 상자입니다.

이것을 해결하는 방법에 대한 아이디어가 있습니까? 내 이미지를 부적절하게 구성하고 있습니까?

도움이 되었습니까?

해결책

HTTP 코드가 정확해 보입니다. 로드가 완료되면 수신 된 데이터의 크기를 기록하고 서버의 이미지의 예상 크기와 비교할 수 있습니다. 예상 크기라면 이미지 자체가 서버에서 손상되었을 수 있습니다.

다른 팁

ASI-HTTP는이 문제를 해결할 수 있습니다.

NSURL *coverRequestUrl = [NSURL URLWithString:imageStringURL];
ASIHTTPRequest *coverRequest = [[ASIHTTPRequest alloc] initWithURL:coverRequestUrl];
[coverRequest setDelegate:self];
[coverRequest setDidFinishSelector:@selector(imageRecieved:)];

[appDelegate.queue addOperation:coverRequest];
[appDelegate.queue go];

AppDelegate의 큐 변수는 asinetwork 큐 객체입니다. 비동기 요청을 보내기 때문에 사용합니다.

- (void)imageRecieved:(ASIHTTPRequest *)response
{
    UIImage *myImage = [UIImage imageWithData:[response responseData]];
}

NSMutabledictionary를 사용 하여이 문제를 해결했습니다.

NSMutableDictionary *dataDictionary;

로드 데이터 기능에서 데이터를 정의합니다.

NSMutableData *receivedData = receivedData = [[NSMutableData alloc] init];

그런 다음 키가 [TheConnection Description] 인 사전에 데이터를로드하고 객체는 내 데이터입니다.

[dataDictionary setObject:receivedData forKey:[theConnection description]];

이렇게하면 대의원에서는 대의원에게 전달 된 연결에 대한 올바른 데이터 객체를 찾아 올바른 데이터 인스턴스에 저장할 수 있습니다. 그렇지 않으면 JPEG MUNGING/손상 문제로 끝납니다.

didreceivedata에서는 다음과 같습니다.

//get the object for the connection that has been passed to connectionDidRecieveData and that object will be the data variable for that instance of the connection.
NSMutableData *theReceivedData = [dataDictionary objectForKey:[connection description]];

//then act on that data
[theReceivedData appendData:data];

마찬가지로 Didreceiversonse에서는 다음과 같습니다.

NSMutableData *theReceivedData = [dataDictionary objectForKey:[connection description]];
[theReceivedData setLength:0];

그리고 연결에서 didfinishloading : nsmutabledata *thereceivedData = [dataDictionary ObjectForKey : [Connection Description]]; img = [[uiimage alloc] initwithdata : thereceivedData];

그리고 이것은 매우 잘 작동하는 것 같습니다. 그건 그렇고, 내 코드는 기반입니다 NsurlConnection에 대한 Apple의 튜토리얼 개별 연결을 추적하기 위해 NSMutabledictionary를 추가하면됩니다. 이게 도움이 되길 바란다. 전체 이미지 처리 코드를 게시하려면 알려주세요.

나는 이것도 보았다. 데이터를 파일에 저장 한 다음 데이터를 이미지로 다시 읽으면 완벽하게 작동합니다. JPEG 이미지 데이터에 HTTP 헤더 정보가 있다고 생각합니다.

파일에 대한 저장 해결 방법이 짜증나기 때문에 누군가가 이것에 대한 해결책을 찾기를 바랍니다.

// problem

UIImage *newImage = [UIImage imageWithData:receivedData];

// crappy workaround

[receivedData writeToFile:[NSString stringWithFormat:@"a.jpg"] atomically:NO];
UIImage *newImage = [UIImage imageWithContentsOfFile:@"a.jpg"];

NSDATA 컨텐츠를 사용하여 복사합니다 receivedData = [NSData dataWithBytes:receivedData.bytes length:receivedData.length] 도움이 될 수 있습니다 (디스크를 저장하고 읽는 것보다 더 효율적입니다).

이에 대한 가능한 이유는 원래 수신자와 객체가 내용을 유지하지 않기 때문입니다 (예 : [NSData dataWithBytesNoCopy:length:]) 그리고 당신은 그들이 풀려 난 후에 그들을 읽으려고 노력합니다.

이것은 당신이 생성 한 스레드의 다른 스레드 에서이 문제를 발견했을 때입니다. NSData 물체.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top