2015-09-22 9 views
13

पर writeToFile बुला रहा NSURLSessionDownloadTask के लिए एक पूरा होने के ब्लॉक में निम्न कोड के साथ एक फ़ाइल में डेटा लिखने की कोशिश कर रहा हूँ:त्रुटि: फ़ाइल ... मौजूद नहीं है जब imageData

void (^completionBlock)(NSURLResponse *response, NSURL *filePath, NSError *error) = ^void (NSURLResponse *response, NSURL *filePath, NSError *error) { 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      if (!error){ 
       NSData *imageData = [NSData dataWithContentsOfURL:filePath]; 
       if(imageData) NSLog(@"image is not null"); 

       if(pic == 1) self.imageView.image = [UIImage imageWithData:imageData]; 
       else if(pic==2) self.imageView2.image = [UIImage imageWithData:imageData]; 

       NSArray *paths = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]; 
       NSURL *documentsDirectoryURL = [paths lastObject]; 
       NSURL *saveLocation; 
       if(pic == 1) saveLocation = [documentsDirectoryURL URLByAppendingPathComponent:self.pictureName1]; 
       else if (pic == 2) saveLocation = [documentsDirectoryURL URLByAppendingPathComponent:self.pictureName2]; 
       else saveLocation = [documentsDirectoryURL URLByAppendingPathComponent:self.pictureName3]; 

       NSLog(@"for # %d writing to file %@", pic, saveLocation); 

       NSError *error = nil;     
       [imageData writeToFile:[saveLocation absoluteString] options:NSAtomicWrite error: &error]; 
       if(error){ 
        NSLog(@"FAILED\n\n\n %@ \n\n\n", [error description]); 
       } 
    } 

मैं कर रहा हूँ डाउनलोड की गई छवियों को UIImageViews में प्रदर्शित करें और imageData पर मेरी शून्य जांच यह पुष्टि करती है कि यह शून्य नहीं है। हालांकि, जब मैं एक फाइल करने के लिए डेटा लिखने के लिए प्रयास करते हैं, मेरे NSLog बाहर निम्न त्रुटि प्रिंट यह दर्शाता है कि लिखने में विफल रहा है:

(log statements) 
# 3 writing to file file:///var/mobile/Containers/Data/Application/3743A163-7EE1-4A5A-BF81-7D1344D6DA45/Documents/pic3.png 
Error Domain=NSCocoaErrorDomain Code=4 "The file “pic1.jpg” doesn’t exist." 
UserInfo={NSFilePath=file:///var/mobile/Containers/Data/Application/3743A163-7EE1- 
4A5A-BF81-7D1344D6DA45/Documents/pic1.jpg, NSUnderlyingError=0x16d67200 {Error 
Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}} 

मैं है तो यह त्रुटि संदेश का संकेत पर एक और सवाल ढूँढने में सक्षम नहीं किया गया यह फ़ाइल, और मुझे त्रुटि संदेश काफी counterintuitive मिल रहा है। मेरी त्रुटि कहां है?

+0

बजाय उन यूआरएल से संबंधित एक फाइल से संबंधित एपीआई का उपयोग करें। या कम से कम जांचें कि '[saveLocation absolutString]' क्या है और यदि निर्देशिका मौजूद है। – trojanfoe

+0

@trojanfoe फ़ाइल से संबंधित एपीआई बेहतर क्यों हैं? – helloB

+0

क्योंकि यह एक फ़ाइल-संबंधित मान है जिसके परिणामस्वरूप आप चाहते हैं ('लिखने के लिए' फ़ाइल: ')। – trojanfoe

उत्तर

37

[saveLocation absoluteString] के बजाय, [saveLocation path] का उपयोग करें। असल में पूर्व आपको "फ़ाइल: /// पथ/फ़ाइल नाम" देता है जबकि बाद वाला आपको "/ पथ/फ़ाइल नाम" देता है, जो सही प्रारूप है।

+1

आपको पता नहीं है कि मैंने इस पर कितना समय बिताया है। बहुत बहुत धन्यवाद। – atulkhatri

5

गॉट Woking .. आप नीचे दिए गए तेज कोड @superstart धन्यवाद:

let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first 
let fileNameString = fileURL.absoluteString.stringByReplacingOccurrencesOfString("/", withString: ""); 
let destinationUrl = documentsUrl!.URLByAppendingPathComponent("check.m4a") 

let request: NSURLRequest = NSURLRequest(URL: fileURL) 
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) 
{ (response: NSURLResponse?, datas: NSData?, error: NSError?) in 
    if error == nil 
    { 
     // datas?.writeToFile(destinationUrl.absoluteString, atomically: false); 

     do 
     { 
      let result = try Bool(datas!.writeToFile(destinationUrl.path!, options: NSDataWritingOptions.DataWritingAtomic)) 
      print(result); 
     } 
     catch let errorLoc as NSError 
     { 
      print(errorLoc.localizedDescription) 
     } 
    } 
    else 
    { 
     print(error?.localizedDescription); 
    } 
} 
संबंधित मुद्दे