【问题标题】:Photo replaces when save to Directory保存到目录时替换照片
【发布时间】:2013-08-17 12:29:41
【问题描述】:

我正在使用代码

NSArray *directoryNames = [NSArray arrayWithObjects:@"hats",@"bottoms",@"right",@"left",nil];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder

    for (int i = 0; i < [directoryNames count] ; i++) {
        NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:[directoryNames objectAtIndex:i]];
        if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
            [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil]; //Create folder

        NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"hats"]; // "right" is at index 2, per comments & code
        NSString *filePath = [folderPath stringByAppendingPathComponent:@"IMAGE_NAME_HERE.PNG"]; 
        NSData *imageData = UIImagePNGRepresentation(captureImage.image);
        [imageData writeToFile:filePath atomically:YES];
    }

要保存,但它总是会自动替换,所以我无法保存多张照片。我应该怎么做才能让它停止这样做而不复制自己?

【问题讨论】:

    标签: ios directory save


    【解决方案1】:

    根据此代码,您总是保存到同一个地方:

    NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"hats"]; // "right" is at index 2, per comments & code
            NSString *filePath = [folderPath stringByAppendingPathComponent:@"IMAGE_NAME_HERE.PNG"]; 
            NSData *imageData = UIImagePNGRepresentation(captureImage.image);
            [imageData writeToFile:filePath atomically:YES];
    

    遍历数组只是每次都将所有内容保存到“帽子”路径。您已经通过 dataPath 对象根据数组对象计算存储图像的位置。你只是没有做任何事情。而是这样做:

    for (int i = 0; i < [directoryNames count] ; i++) {
            NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:[directoryNames objectAtIndex:i]];
            if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
                [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil]; //Create folder
            [imageData writeToFile:dataPath atomically:YES];
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-10
      • 1970-01-01
      • 2018-04-10
      • 1970-01-01
      • 1970-01-01
      • 2021-04-21
      相关资源
      最近更新 更多