पर 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 मिल रहा है। मेरी त्रुटि कहां है?
बजाय उन यूआरएल से संबंधित एक फाइल से संबंधित एपीआई का उपयोग करें। या कम से कम जांचें कि '[saveLocation absolutString]' क्या है और यदि निर्देशिका मौजूद है। – trojanfoe
@trojanfoe फ़ाइल से संबंधित एपीआई बेहतर क्यों हैं? – helloB
क्योंकि यह एक फ़ाइल-संबंधित मान है जिसके परिणामस्वरूप आप चाहते हैं ('लिखने के लिए' फ़ाइल: ')। – trojanfoe