문제

사용 중입니다 initwithcontentsofurlNSDATA URL에서 이미지를로드합니다. 그러나 이미지의 크기를 미리 알지 못하며 응답이 특정 크기를 초과하면 중지 또는 실패를 연결하고 싶습니다.

이것을 할 수있는 방법이 있습니까? iPhone 3.0?

미리 감사드립니다.

도움이 되었습니까?

해결책

그러나 NSData를 통해 직접 할 수는 없습니다 nsurlconnection 이미지를 비동기로로드하고 사용하여 그러한 것을 지원할 것입니다. 연결 : didreceivedata : 받은 데이터의 양을 확인하려면 한도를 넘어 서면 취소 요청을 중지하기 위해 nsurlconnection으로 메시지.

간단한 예 : (수신자는 헤더에 nsmutabledata로 정의됩니다)

@implementation TestConnection

- (id)init {
    [self loadURL:[NSURL URLWithString:@"http://stackoverflow.com/content/img/so/logo.png"]];
    return self;
}

- (BOOL)loadURL:(NSURL *)inURL {
    NSURLRequest *request = [NSURLRequest requestWithURL:inURL];
    NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];

    if (conn) {
        receivedData = [[NSMutableData data] retain];
    } else {
        return FALSE;
    }

    return TRUE;
}

- (void)connection:(NSURLConnection *)conn didReceiveResponse:(NSURLResponse *)response {
    [receivedData setLength:0]; 
}

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

    if ([receivedData length] > 5120) { //5KB
        [conn cancel];
    }
}

- (void)connectionDidFinishLoading:(NSURLConnection *)conn {
    // do something with the data
    NSLog(@"Succeeded! Received %d bytes of data", [receivedData length]);

    [receivedData release];
}

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