下面的代码工作没有错误或异常-但仍然,它没有做它应该做的!我想将图像保存到iOS库/应用程序支持文件夹中。更准确地说,应该将图像放置到/library/Application Support/bundleID_name/子文件夹/(以及称为“location1”的子文件夹中)。
如果我使用iOS-模拟器检查功能,我可以看到子文件夹的创建(即./library/Application Support/bundleID_name/location1/)。此外,"saveImage“函数也毫无例外地工作。但是没有图像被保存!(i.e.the图像文件丢失,文件夹保持为空)!
什么可能是错误??
下面是调用两个函数的代码(请参阅下面的代码):
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");
}顺便说一下,使用XCode-Debugger获得"fullPath“,我得到:
"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内容:imageData属性:无;未执行?
发布于 2014-08-01 14:43:47
这是:
"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,则使用ULRs,而不是尝试转换为字符串:
[imageData writeToURL:pathName atomically:YES];(最好将参数命名为pathURL),如果您想使用路径,那么在任何阶段都不要使用URL。
此外,如果API方法返回错误或状态标志,请在代码中作为标准检查它。
发布于 2014-08-01 14:37:19
我很确定你不能在你指定的路径上保存一个图像。您可以在图库或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];https://stackoverflow.com/questions/25082517
复制相似问题