당신은 단순히 NSOperation
를 서브 클래 싱 수행 다운로드하고 NSOperation
서브 클래스의 그리고 당신의 작업 후 -main
방법으로 파일 시스템 데이터를 저장하기위한 코드를 넣을 수 있습니다, NSOperationQueue
의 다음 작업은
@implementation MyOperation
- (void) main {
if ([self isCancelled]) {
NSLog(@"** operation cancelled **");
} else {
NSURL *conUrl = [NSURL URLWithString: urlString];
NSError *error;
NSData *conData = [NSData dataWithContentsOfURL: conUrl options: NSDataReadingMappedIfSafe error: & error];
if (!error) {
UIImage *image = [
[UIImage alloc] initWithData: conData];
NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex: 0];
NSString *jpgFilePath = [NSString stringWithFormat: @"%@/%@.jpg", cacheDir, textLabel];
conData = [NSData dataWithData: UIImageJPEGRepresentation(image, 1.0f)];
[conData writeToFile: jpgFilePath atomically: YES];
}
}
NSLog(@"Operation finished");
}
@end
자동으로 실행을 시작합니다 나는 확실하지 않다. 그러나 당신은이 목적을 위해 중첩 된 디스패치 큐를 사용할 수있다. 쉽고 적은 코드가 될 것이다. –
@iVishal 내가 걱정하는 한, NSOperationQueue는 GCD 위에 구축되므로 기본적으로 같은 것이다. . –