我正在尝试将图像保存到我的应用文档目录中。虽然我可以轻松地将图像保存到目录中,但它需要大约两秒钟来保存图像及其缩略图。我使用以下方法使用队列保存图像。
+ (void)saveImage:(UIImage*)imageToSave withName:(NSString*)imageName toFolder:(NSString*)folderName
{
[Utils createFolderWithPath:folderName];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@.png",folderName,imageName]];
NSData *imageData = UIImagePNGRepresentation(imageToSave);
[imageData writeToFile:savedImagePath atomically:NO];
}
+ (void)createFolderWithPath:(NSString *)folderPath
{
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:folderPath];
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
[[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:NO attributes:nil error:&error];
}
}
有更快的方法吗?由于我必须保存多张图像,每张图像需要2秒钟才能完成!
答案 0 :(得分:0)
您不需要每次都找到文档目录。只需找出一次并存储它。并在线程中运行代码,线程将图像保存在后台。