Xcode 6 keeps renaming my app's directory in iOS8 simulator after each run.

后端 未结 5 764
情歌与酒
情歌与酒 2020-11-29 04:05

I\'m running Xcode 6 Beta 5 but this has been happening since the first beta. My app\'s directory in the simulator keeps being renamed after each run. It took me a while to

5条回答
  •  醉梦人生
    2020-11-29 05:00

    You need to to save only path inside DocumentDirectory(directory/file name), and add it to the DocumentDirectory every time you load the file...

    -(void)saveImage:(UIImage *)image{
    NSData *pngData = UIImagePNGRepresentation(image);
    NSString *pathInDocumentDirectory = [APP_DocumentDirectory stringByAppendingPathComponent:PROFILE_IMAGE_NAME];
    NSString *filePath = [self documentsPathForFileName:pathInDocumentDirectory];
    //Save pic file path - DirName/Filename.png
    [XYZPreferencesHelper setUserImageFilePath:pathInDocumentDirectory];
     //Write the file
    [[NSFileManager defaultManager] createFileAtPath:filePath contents:pngData attributes:nil];
    
    }
    
    
    
    -(void)loadSavedUserPicture{
    //Load saved DirName/Filename.png
    NSString *pathInDocumentDirectory = [XYZPreferencesHelper getUserImageFilePath];
    
    if (pathInDocumentDirectory != nil){
        //Full path with new app Document Directory
        NSString *filePath = [self documentsPathForFileName:pathInDocumentDirectory];
        if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]){
            NSData *pngData = [NSData dataWithContentsOfFile:filePath];
            UIImage *image = [UIImage imageWithData:pngData];
            if (image != nil){
                userPicImageView.image = image;
            }
    
        }
    
     }
    }
    
    
    
    - (NSString *)documentsPathForFileName:(NSString *)name
    {
    NSString *documentsPath = [self createRestcallDirectoryIfNotExist];
    return [documentsPath stringByAppendingPathComponent:name];
    }
    
    
    
    -(NSString *)createRestcallDirectoryIfNotExist{
    NSString *path;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    path = [documentsPath stringByAppendingPathComponent:APP_DocumentDirectory];
    NSError *error;
    if (![[NSFileManager defaultManager] fileExistsAtPath:path])    //Does directory already exist?
    {
        if (![[NSFileManager defaultManager] createDirectoryAtPath:path
                                       withIntermediateDirectories:NO
                                                        attributes:nil
                                                             error:&error])
        {
            NSLog(@"Create directory error: %@", error);
        }
    }
    return documentsPath;
    }
    

提交回复
热议问题