简体   繁体   中英

Unable to write NSMutableArray to NSFileManager iOS Sdk

i wrote below code to write the nsmutablearray to filemanager.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

// the path to write file
NSString *arrayFile = [documentsDirectory stringByAppendingPathComponent:arrayName];
[imagesarray writeToFile:arrayFile atomically:YES];

but the file id not creating in NSFilemanager and i am not getting what might be the problem is.

can any one please tell me the reason.

There are several things that may cause this piece of code to fail. The things to check are: 1) Make sure that the value of arrayFile is a correct file name in the absolute path of your application sandbox. It cannot in the app bundle or outside the sandbox. 2) Make sure that all the elements in the imagesArray are writable via this method. This method will only write NSString, NSNumber, NSDate, NSData, NSArray, and NSDictionary objects. If the object is an NSDictionary or NSArray then each every element or sub element must also be one of those four types.

The name of the array implies that you're writing an array of images. That will not work because UIImage is not one of these sacred 6 types. You can convert a UIImage to JPEG or PNG representation using a snippet like the following:

UIImage *image = [UIImage imageNamed:@"myimage.png"];
NSData *data = UIImagePNGRepresentation(image, 1.0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM