2012-09-12 3 views
0

제대로 작동하려면 사운드 파일을 다운로드해야하는 앱이 있습니다.NsurlConnection을 사용하여 다운로드를 시작하면 앱이 중단됩니다.

크기가 20Mb 이상인 파일을 비동기 적으로 다운로드하기 위해 NSURLConnection을 사용하고 있습니다. 다운로드 비율을 추적하기 위해 progressBarView을 배치했으며 Apple에서 제안한대로 NSUrlConnection이라는 위임 방법을 사용하고 있습니다.

NSURLRequest *theRequest=[NSURLRequest requestWithURL:soundFileURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0]; 
// create the connection with the request 
// and start loading the data 
NSURLConnection *theConnection; 

theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

//[theConnection cancel]; 
//[theConnection start]; 
if (theConnection) { 
    // Create the NSMutableData that will hold 
    // the received data 
    // receivedData is declared as a method instance elsewhere 
    receivedData=[[NSMutableData data] retain]; 
} else { 
    // inform the user that the download could not be made 
} 

위임 방법

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

하고 그래서

... 나는 다운로드를 시작하면, 인터페이스가 때때로 중단하고 progressView뿐만 아니라 중단

.

한 가지 주목할만한 점은 아마도 또 다른 질문입니다. 다운로드가 완료 될 때까지 기다려야하므로 사용자 인터페이스가 비활성화되고 물론 그에게 메시지를 전합니다. 애플이 내 앱을 거부하겠습니까? 내 질문 : 기본적으로 메인 스레드에 NSURLConnectionDelegate

+0

추가 활동 표시를 추가하고 다운로드 –

답변

1

NSUrlConnection 전송 이벤트를 읽어 주셔서 감사 그

에 대해 몹시 걱정입니다. 이 연결에 대해 새 풀 및 runloop을 만들어야하며 백그라운드에서이 풀을 처리해야합니다. 다음은 백그라운드에서 이미지를 다운로드하는 예입니다. 수정 된 NSOperationQueue 및 NSOperation을 사용하지만 파일을 쉽게 수정할 수 있습니다. LinkedImageFetcher on developer.apple.com

+0

당신을 감사 완료되면 animationg을 중지, 내가 문제가 무엇인지 알아 냈어! didreceivedata 대리자 메서드 내가 느리게 만든 파일에 데이터를 쓰고있었습니다! 그리고 예 그것은 어리 석다! 그냥 [ercdata writeToFile : fileDir automatically : YES];로 옮깁니다. didfinish 대리자 메서드에 . –

0
//1 First allocate NSOperationQueue object and set number of concurrent operations to execute at a time 

NSOperationQueue *thumbnailQueue = [[NSOperationQueue alloc] init]; 
    thumbnailQueue.maxConcurrentOperationCount = 3; 
// load photo images in the background 
    __weak BHCollectionViewController *weakSelf = self; 
    NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ 
     UIImage *image = [photo image]; 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      // then set them via the main queue if the cell is still visible. 

       cell.imageView.image = image; 
      } 
     }); 
    }]; 

    operation.queuePriority = (indexPath.item == 0) ? 
     NSOperationQueuePriorityHigh : NSOperationQueuePriorityNormal; 

    [thumbnailQueue addOperation:operation]; 

것은 NSObject의 사진 클래스를 만들고 시작 애니메이션 방법 아래

- (UIImage *)image 
{ 
    if (!_image && self.imageURL) { 
     NSData *imageData = [NSData dataWithContentsOfURL:self.imageURL]; 
     UIImage *image = [UIImage imageWithData:imageData scale:[UIScreen mainScreen].scale]; 

     _image = image; 
    } 

    return _image; 
} 
+0

이 답변은 질문과 어떤 관련이 있습니까? – HAS

관련 문제