以下代码在没有错误或异常的情况下工作 - 但是,它仍然没有做它应该做的事情!我想将图像保存到iOS库/ Application Support文件夹中。更准确地说,图像应该放在/ library / Application Support / bundleID_name /子文件夹/(并且子文件夹被调用" location1")。
如果我使用iOS-Simulator检查功能,我可以看到子文件夹的创建(即... / library / Application Support / bundleID_name / location1 /)。功能" saveImage"无例外地工作。但没有图像被保存!!!! (即图像文件丢失且文件夹仍为空)!!
可能是什么错误?
这是我的代码,调用了两个函数(参见下面的代码):
UIImage *in_image = [UIImage imageNamed:@"template009c.jpg"];
NSString *locDirectoryName = @"location1";
NSURL *LocationDirectory = [self appendLocationToApplicationDirectory:locDirectoryName];
[self saveImage:in_image :@"image1" :LocationDirectory];
使用相应的函数-Nr1:
- (NSURL*)appendLocationToApplicationDirectory:(NSString*)locationDirName
{
NSString* appBundleID = [[NSBundle mainBundle] bundleIdentifier];
NSFileManager*fm = [NSFileManager defaultManager];
NSURL* dirPath = nil;
// Find the application support directory in the home directory.
NSArray* appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory
inDomains:NSUserDomainMask];
if ([appSupportDir count] > 0) {
// Append the bundle ID and the location-Foldername to the URL for the Application Support directory
dirPath = [[[appSupportDir objectAtIndex:0] URLByAppendingPathComponent:appBundleID] URLByAppendingPathComponent:locationDirName];
// If the directory does not exist, this method creates it.
// This method call works in OS X 10.7 and later only.
NSError* theError = nil;
if (![fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES attributes:nil error:&theError]) {
// Handle the error.
NSLog(@"%@", theError.localizedDescription);
return nil;
}
else {
// Mark the directory as excluded from iCloud backups
if (![dirPath setResourceValue:@YES
forKey:NSURLIsExcludedFromBackupKey
error:&theError]) {
NSLog(@"Error excluding %@ from iCloud backup %@", [dirPath lastPathComponent], theError.localizedDescription);
}
else {
NSLog(@"Location Directory excluded from iClud backups");
}
}
}
return dirPath;
}
功能Nr2:
//saving an image
- (void)saveImage:(UIImage*)image :(NSString*)imageName :(NSURL*)pathName {
NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.
NSFileManager *fileManager = [NSFileManager defaultManager];
// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *LocationDirectory = [pathName absoluteString];
NSString *fullPath = [LocationDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]];
/***** THE FOLLOWING LINE DOES NOT SEEM TO DO WHAT IT IS SUPPOSED TO *******/
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil];
/**** I also tried without the FileManager, but same problem - no file written... ***/
// [imageData writeToFile:fullPath atomically:NO];
NSLog(@"image saved");
}
顺便说一句,获得" fullPath"使用XCode-Debugger,我得到:
"fullPath NSPathStore2 * @"file:/Users/username/Library/Application%20Support/iPhone%20Simulator/7.1/Applications/2BCC3345-9M55F-4580-A1E7-6694E33456777/Library/Application%20Support/bundleID_name/image1.png" 0x09d50950
这看起来也不正确吗?但是为什么[fileManager createFileAtPath:fullPath contents:imageData attributes:nil];不履行???
答案 0 :(得分:0)
我很确定您无法在指定的路径上保存图像。您可以在图库或DocumentDirectory中保存图像。这应该是在DocumentDirectory上保存图像的代码:
NSString *imgName=[@"imgname.png"];
[[NSUserDefaults standardUserDefaults]setValue:imgName forKey:@"imageName"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:imgName];
UIImage *image = imageView.image; // imageView is my image from camera
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
答案 1 :(得分:0)
此:
"fullPath NSPathStore2 * @"file:/Users/username/Library/Application%20Support/iPhone%20Simulator/7.1/Applications/2BCC3345-9M55F-4580-A1E7-6694E33456777/Library/Application%20Support/bundleID_name/image1.png" 0x09d50950
不是有效路径,它是一个URL路径,但存储在一个字符串中。如果您打算使用URL,则使用ULR而不是尝试转换为字符串:
[imageData writeToURL:pathName atomically:YES];
(最好将参数命名为pathURL
),如果您想使用路径,请不要在任何阶段使用URL。
此外,如果API方法返回错误或状态标志,请在代码中将其作为标准进行检查。