Nsfilemanager gets stuck when initializing a file.

Source: Internet
Author: User

Debug a program today. The content is to generate a cache file in a local path on the mobile phone. When the local path is generated, an error is made. In the spirit of seeking for reasons, debug the program for two hours, I finally found the reason.

At the beginning of breakpoint debugging, execute row 13th. No data is written here, and no is always returned. As a result, I am all broken, and then the printed path is under the library, I wonder if it's not here

Allow the user to create a data cache here, and change the nsdocumentationdirectory of two rows to nsdocumentdirectory, and then the path is changed:

File: // var/mobile/applications/79d7dd73-74c5-4ece-bef7-5988560a8ac2/documents/436783070; then the path here can write data ..

 

Before modification:

1-(nsurl *) uniquedocumenturl {2 nsarray * documentdictiories = [[nsfilemanager defaultmanager] urlsfordirectory: nsdocumentationdirectory indomains: nsuserdomainmask]; // note nsdocumentationdirectory 3 nsstring * unique = [nsstring stringwithformat: @ "%. 0f ", floor ([nsdate timeintervalsincereferencedate])]; 4 ID urlobject = [documentdictiories firstobject]; 5 return [urlobject urlbyappendingpathcomponen T: Unique]; 6} 7 8-(nsurl *) imageurl {9 If (! _ Imageurl & self. image) {10 nsurl * url = [self uniquedocumenturl]; 11 if (URL) {12 nsdata * imagedata = uiimagejpegrepresentation (self. image, 1.0); 13 if ([imagedata writetourl: URL atomically: Yes]) {// Question 14 _ imageurl = URL; 15} 16} 17} 18 return _ imageurl; 19} 20 21 // print 22 printing description of URL on the console: 23 file: // var/mobile/applications/16614ff0-b641-4938-8bf8-91658759d4d8/library/documentation/436783128

After modification:

 1 - (NSURL *)uniqueDocumentURL 2 { 3     NSArray *documentDirectories = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]; 4     NSString *unique = [NSString stringWithFormat:@"%.0f", floor([NSDate timeIntervalSinceReferenceDate])]; 5     return [[documentDirectories firstObject] URLByAppendingPathComponent:unique]; 6 } 7  8 - (void)setImage:(UIImage *)image 9 {10     self.imageView.image = image;11 12     // when image is changed, we must delete files we‘ve created (if any)13     [[NSFileManager defaultManager] removeItemAtURL:_imageURL error:NULL];14     [[NSFileManager defaultManager] removeItemAtURL:_thumbnailURL error:NULL];15     self.imageURL = nil;16     self.thumbnailURL = nil;17 }18 19 20 Printing description of url:21 file:///var/mobile/Applications/79D7DD73-74C5-4ECE-BEF7-5988560A8AC2/Documents/436783070

 

Nsfilemanager gets stuck when initializing a file.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.