iphone - 如何将 UIImage 上传到文档中的特定文件夹?

标签 iphone objective-c ios

我有一个图像名称(可以说是@"image.jpg"),我想将它保存到我在名为“coffeeShops”的文档文件夹中创建的文件夹中。我该怎么做?

最佳答案

NSString *docs = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
NSString *dir = @"coffeeShops";
NSString *destPath = [docs stringByAppendingPathComponent:dir];

// check if the destination directory exists, if not create it
BOOL isDirectory;
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:destPath isDirectory:&isDirectory];
if(!exists || !isDirectory) {
    NSError *error = nil;
    [[NSFileManager defaultManager] createDirectoryAtPath:destPath withIntermediateDirectories:NO attributes:nil error:&error];
    if(error != nil) {
        // should do error checking here
        NSLog(@"%@",[error localizedDescription]);
    }
}

NSString *fileName = @"image.jpg";
NSString *path = [destPath stringByAppendingPathComponent:fileName];

[UIImageJPEGRepresentation(image) writeToFile:path atomically:YES];

关于iphone - 如何将 UIImage 上传到文档中的特定文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6704364/

相关文章:

iphone - iPhone 的定位精度会超出职责范围吗?

iphone - 关于 CSS % height 的问题

iphone - 应用会加载默认屏幕,然后变黑或崩溃

ios - 在我关于 tabbarcontroller 和 dismissviewcontroller 的项目中

objective-c - 使用 performSelectorInBackground 的风险?

ios - 更改 UITabBar 高度

iOS 企业配置文件到期

Objective-C – cellForRowAtIndexPath 导致按钮框架改变大小?

iphone - iOS 文件系统 HFS?

ios - 消耗品订阅和不可续订订阅之间有什么区别?