2012-06-25 15 views
12

मैंने सोचा कि मैंने यह पता लगाया है लेकिन मैं इसे काम पर नहीं ला सकता हूं। मेरे पास एक विधि है जिसे किसी सरणी में प्रत्येक यूआरएल पर बुलाया जाता है। इस विधि में एक तस्वीर का URL है जिसे ऑफ़लाइन उपयोग के लिए एप्लिकेशन समर्थन फ़ोल्डर में विशिष्ट पथ में डाउनलोड किया जाना चाहिए। लेकिन शायद मैं AFNetwork लाइब्रेरी में विधियों की गलत व्याख्या कर रहा हूं। मेरे विधि इस प्रकार है:आईओएस में AFNetworking के साथ एक फ़ाइल/छवि डाउनलोड करें?

- (void) downloadImageInBackground:(NSDictionary *)args{ 

    @autoreleasepool { 

    NSString *photourl = [args objectForKey:@"photoUrl"]; 
    NSString *articleID = [args objectForKey:@"articleID"]; 
    NSString *guideName = [args objectForKey:@"guideName"]; 
    NSNumber *totalNumberOfImages = [args objectForKey:@"totalNumberOfImages"]; 

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:photourl]]; 

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
    operation.inputStream = [NSInputStream inputStreamWithURL:[NSURL URLWithString:photourl]]; 

    [operation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{ 
     DLog(@"PROBLEMS_AF"); 
    }]; 
    DLog(@"URL_PHOTOURL: %@", photourl); 
    DLog(@"indexSet: %@", operation.hasAcceptableStatusCode); 
    [operation response]; 

    NSData *data = [args objectForKey:@"data"]; 

    NSString *path; 
    path = [NSMutableString stringWithFormat:@"%@/Library/Application Support/Guides", NSHomeDirectory()]; 
    path = [path stringByAppendingPathComponent:guideName]; 
    NSString *guidePath = path; 
    path = [path stringByAppendingPathComponent:photourl]; 

    if ([[NSFileManager defaultManager] fileExistsAtPath:guidePath]){ 
     [[NSFileManager defaultManager] createFileAtPath:path 
               contents:data 
               attributes:nil]; 
    } 

    DLog(@"path: %@", path); 
    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO]; 
    [operation start]; 


    DLog(@"isExecuting: %d",[operation isExecuting]); 
    DLog(@"IS_FINISHED: %d",[operation isFinished]); 


    } 

} 

PhotoURL छवि है कि मैं डाउनलोड करना चाहते करने के लिए सीधा संबंध है।

चूंकि इस विधि को सभी छवियों के लिए बुलाया जाता है, इसलिए सभी लॉग कई बार बुलाए जाते हैं, और यह सही लगता है।

उत्तर

39

आपको यहां कुछ समस्याएं हैं। तो सबसे पहले, आप @autoreleasepool का उपयोग क्यों कर रहे हैं? मुझे लगता है कि यहां इसकी कोई आवश्यकता नहीं है। क्या आप एआरसी का उपयोग कर रहे हैं? मैं इसे अपने बाकी उत्तर के लिए मानता हूं।

AFNetworking में AFImageRequestOperation नामक एक कक्षा है, इसलिए यह आपके लिए उपयोग करना एक अच्छा विचार होगा। पहले, आयात यह

#import "AFImageRequestOperation.h" 

तो आप अब एक वस्तु

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:photourl]]; 
AFImageRequestOperation *operation; 
operation = [AFImageRequestOperation imageRequestOperationWithRequest:request 
    imageProcessingBlock:nil 
    cacheName:nil 
    success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 

    } 
    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { 
     NSLog(@"%@", [error localizedDescription]); 
    }]; 

बना सकते हैं, सफलता ब्लॉक में, आप UIImage आप की जरूरत मिल गया। वहां आपको दस्तावेज़ निर्देशिका प्राप्त करने की आवश्यकता है। आपका कोड आईओएस डिवाइस पर काम नहीं करेगा।

// Get dir 
NSString *documentsDirectory = nil; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
documentsDirectory = [paths objectAtIndex:0]; 
NSString *pathString = [NSString stringWithFormat:@"%@/%@",documentsDirectory, guideName]; 

और फिर आप इस्तेमाल कर सकते हैं NSDatas writeToFile

// Save Image 
NSData *imageData = UIImageJPEGRepresentation(image, 90); 
[imageData writeToFile:pathString atomically:YES]; 

अंत में, आप आपरेशन

[operation start]; 

सब एक साथ शुरू करने की आवश्यकता:

- (void)downloadImageInBackground:(NSDictionary *)args{ 

    NSString *guideName = [args objectForKey:@"guideName"]; 
    NSString *photourl = [args objectForKey:@"photoUrl"]; 

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:photourl]]; 

    AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request 
     imageProcessingBlock:nil 
     cacheName:nil 
     success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 

      // Get dir 
      NSString *documentsDirectory = nil; 
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
      documentsDirectory = [paths objectAtIndex:0]; 
      NSString *pathString = [NSString stringWithFormat:@"%@/%@",documentsDirectory, guideName]; 

      // Save Image 
      NSData *imageData = UIImageJPEGRepresentation(image, 90); 
      [imageData writeToFile:pathString atomically:YES]; 

     } 
     failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { 
      NSLog(@"%@", [error localizedDescription]); 
     }]; 

    [operation start]; 
} 
+0

तुम मुझे सीधे इशारा किया । मैं जो कर रहा था उसके बारे में कुछ संदेह था, बहुत बहुत धन्यवाद। –

+2

अतिरिक्त: यदि आपके पास एक साथ कई अनुरोध हैं, तो 'NSOperationQueue' (सभी AFRequestOperation क्लासेस कोडिंग अनुपालन कर रहे हैं) का उपयोग करने पर विचार करें और कतार संचालन को संभालने दें। यह आसान हो सकता है और एक ही समय में 100 अनुरोधों को आग नहीं लगाएगा। – choise

+0

एनएसओपरेशनक्यूयू को लागू किए बिना आमतौर पर कितने अनुरोध किए जा सकते हैं ?, यह 50 से अधिक अनुरोध कभी नहीं होगा, क्या यह पर्याप्त है कि यह लागू करने योग्य है? –

5

यहां समस्या यह है कि ऑपरेशन को बनाए रखा नहीं जाता है। इसे तत्काल हटा दिया जाएगा।

या तो ऑपरेशन को अपनी कक्षा की संपत्ति बनाएं या ऑपरेशन कतार (एक संपत्ति भी) आपके लिए अनुरोध (अनुशंसित) को संभालने दें। बाद के मामले में [operation start] पर कॉल न करें। जब आप AFHTTPClient का उपयोग करते हैं तो ऑपरेशन कतार भी आपके लिए प्रबंधित की जाएगी।

इसके अलावा आपको अनुरोध ऑपरेशन (setCompletionBlockWithSuccess:failure:) के लिए एक पूर्ण कॉलबैक पंजीकृत करना चाहिए।

संबंधित मुद्दे