Copy images from an app to Documents Directory and rewrite them after it

房东的猫 提交于 2020-01-05 03:43:11

问题


I have an app where I have a certain amount of .jpg pictures (roughly 300). They serve as something to start with, because they're actually located in the internet, but obviously it's more convenient for user not to download all of them at the first start of the App, but to have them pre-packed.

I am required to rewrite these images everytime I get new information from server. Obviously, I can't touch the app bundle, so I see my steps like this:

  1. Unpack the images from bundle to the Documents Directory at the first start of an App.
  2. Access them only from Documents Directory, but not from bundle.
  3. If it's necessary, I should rewrite them.

And thus my code will be unified, cause I will always use the same path to get the image.

The problem is that I know very little about the whole file system thing in iOS, so I don't know how to unpack the particular bundle contents to Documents Directory and also I don't know how to write to Documents Directory either.

Could you please help me with some code and also confirm that my solution scheme is right?


回答1:


NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *destPath = [documentsDirectory stringByAppendingPathComponent:@"images"];  //optionally create a subdirectory

//"source" is a physical folder in your app bundle.  Once that has a blue color folder (not the yellow group folder)
// To create a physical folder in your app bundle: drag a folder from Mac's Finder to the Xcode project, when prompts
// for "Choose options for adding these files" make certain that "Create folder references for …" is selected.
// Store all your 300 or so images into this physical folder.

NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"source"];  
NSError *error;
[[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:destPath error:&error];
if (error)
    NSLog(@"copying error: %@", error);

Edited per additional comment from the OP:

To rewrite with the same file name to the same directory, you can use a combination of fileExistsAtPath and removeItemAtPath to detect and remove the existing file before writing.

if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
    [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
}
// now proceed to write-rewrite



回答2:


Try this code

-(void)demoImages
{

//-- Main bundle directory
NSString *mainBundle = [[NSBundle mainBundle] resourcePath];
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = [[NSError alloc] init];
NSArray *mainBundleDirectory = [fm  contentsOfDirectoryAtPath:mainBundle error:&error];

NSMutableArray *images = [[NSMutableArray alloc]init];
for (NSString *pngFiles in mainBundleDirectory)
{
    if ([pngFiles hasSuffix:@".png"])
    {
        [images addObject:pngFiles];
    }
}

NSLog(@"\n\n Doc images %@",images);
//-- Document directory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentDirectory = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];

//-- Copy files form main bundle to document directory
for (int i=0; i<[images count]; i++)
{
    NSString *toPath = [NSString stringWithFormat:@"%@/%@",documentDirectory,[images objectAtIndex:i]];
    [fileManager copyItemAtPath:[NSString stringWithFormat:@"%@/%@",mainBundle,[images objectAtIndex:i]] toPath:toPath error:NULL];
    NSLog(@"\n Saved %@",fileManager);
}


}


来源:https://stackoverflow.com/questions/15119564/copy-images-from-an-app-to-documents-directory-and-rewrite-them-after-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!