IOS之文件夹创建、删除,图片在本地的保存和加载
2012-12-25 14:09
507 查看
本文转自http://blog.csdn.net/toddmi/article/details/8204102
[html] view
plaincopy
// get file absolutely path in the caches directory
NSString* pathInCacheDirectory(NSString *fileName)
{
NSArray *cachePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [cachePaths objectAtIndex:0];
return [cachePath stringByAppendingPathComponent:fileName];
}
[html] view
plaincopy
// get file absolutely path in the caches directory
NSString* pathInCacheDirectory(NSString *fileName)
{
NSArray *cachePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [cachePaths objectAtIndex:0];
return [cachePath stringByAppendingPathComponent:fileName];
}
[html] view
plaincopy
// create directory in the caches directory
bool createDirInCache(NSString *dirName)
{
NSString *imageDir = pathInCacheDirectory(dirName);
BOOL isDir = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL existed = [fileManager fileExistsAtPath:imageDir isDirectory:&isDir];
bool isCreated = false;
if ( !(isDir == YES && existed == YES) )
{
isCreated = [fileManager createDirectoryAtPath:imageDir withIntermediateDirectories:YES attributes:nil error:nil];
}
return isCreated;
}
// delete directory in the caches directory
bool deleteDirInCache(NSString *dirName)
{
NSString *imageDir = pathInCacheDirectory(dirName);
BOOL isDir = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL existed = [fileManager fileExistsAtPath:imageDir isDirectory:&isDir];
bool isDeleted = false;
if ( isDir == YES && existed == YES )
{
isDeleted = [fileManager removeItemAtPath:imageDir error:nil];
}
return isDeleted;
}
// save Image to the caches directory
bool saveImageToCacheDir(NSString *directoryPath, UIImage *image, NSString *imageName, NSString *imageType)
{
BOOL isDir = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL existed = [fileManager fileExistsAtPath:directoryPath isDirectory:&isDir];
bool isSaved = false;
if ( isDir == YES && existed == YES )
{
if ([[imageType lowercaseString] isEqualToString:@"png"])
{
isSaved = [UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];
}
else if ([[imageType lowercaseString] isEqualToString:@"jpg"] || [[imageType lowercaseString] isEqualToString:@"jpeg"])
{
isSaved = [UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
}
else
{
NSLog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", imageType);
}
}
return isSaved;
}
// load Image from caches dir to imageview
NSData* loadImageData(NSString *directoryPath, NSString *imageName)
{
BOOL isDir = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL dirExisted = [fileManager fileExistsAtPath:directoryPath isDirectory:&isDir];
if ( isDir == YES && dirExisted == YES )
{
NSString *imagePath = [directoryPath stringByAppendingString : imageName];
BOOL fileExisted = [fileManager fileExistsAtPath:imagePath];
if (!fileExisted) {
return NULL;
}
NSData *imageData = [NSData dataWithContentsOfFile : imagePath];
return imageData;
}
else
{
return NULL;
}
}
[html] view
plaincopy
// create directory in the caches directory
bool createDirInCache(NSString *dirName)
{
NSString *imageDir = pathInCacheDirectory(dirName);
BOOL isDir = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL existed = [fileManager fileExistsAtPath:imageDir isDirectory:&isDir];
bool isCreated = false;
if ( !(isDir == YES && existed == YES) )
{
isCreated = [fileManager createDirectoryAtPath:imageDir withIntermediateDirectories:YES attributes:nil error:nil];
}
return isCreated;
}
// delete directory in the caches directory
bool deleteDirInCache(NSString *dirName)
{
NSString *imageDir = pathInCacheDirectory(dirName);
BOOL isDir = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL existed = [fileManager fileExistsAtPath:imageDir isDirectory:&isDir];
bool isDeleted = false;
if ( isDir == YES && existed == YES )
{
isDeleted = [fileManager removeItemAtPath:imageDir error:nil];
}
return isDeleted;
}
// save Image to the caches directory
bool saveImageToCacheDir(NSString *directoryPath, UIImage *image, NSString *imageName, NSString *imageType)
{
BOOL isDir = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL existed = [fileManager fileExistsAtPath:directoryPath isDirectory:&isDir];
bool isSaved = false;
if ( isDir == YES && existed == YES )
{
if ([[imageType lowercaseString] isEqualToString:@"png"])
{
isSaved = [UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];
}
else if ([[imageType lowercaseString] isEqualToString:@"jpg"] || [[imageType lowercaseString] isEqualToString:@"jpeg"])
{
isSaved = [UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
}
else
{
NSLog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", imageType);
}
}
return isSaved;
}
// load Image from caches dir to imageview
NSData* loadImageData(NSString *directoryPath, NSString *imageName)
{
BOOL isDir = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL dirExisted = [fileManager fileExistsAtPath:directoryPath isDirectory:&isDir];
if ( isDir == YES && dirExisted == YES )
{
NSString *imagePath = [directoryPath stringByAppendingString : imageName];
BOOL fileExisted = [fileManager fileExistsAtPath:imagePath];
if (!fileExisted) {
return NULL;
}
NSData *imageData = [NSData dataWithContentsOfFile : imagePath];
return imageData;
}
else
{
return NULL;
}
}
[html] view
plaincopy
// get file absolutely path in the caches directory
NSString* pathInCacheDirectory(NSString *fileName)
{
NSArray *cachePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [cachePaths objectAtIndex:0];
return [cachePath stringByAppendingPathComponent:fileName];
}
[html] view
plaincopy
// get file absolutely path in the caches directory
NSString* pathInCacheDirectory(NSString *fileName)
{
NSArray *cachePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [cachePaths objectAtIndex:0];
return [cachePath stringByAppendingPathComponent:fileName];
}
[html] view
plaincopy
// create directory in the caches directory
bool createDirInCache(NSString *dirName)
{
NSString *imageDir = pathInCacheDirectory(dirName);
BOOL isDir = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL existed = [fileManager fileExistsAtPath:imageDir isDirectory:&isDir];
bool isCreated = false;
if ( !(isDir == YES && existed == YES) )
{
isCreated = [fileManager createDirectoryAtPath:imageDir withIntermediateDirectories:YES attributes:nil error:nil];
}
return isCreated;
}
// delete directory in the caches directory
bool deleteDirInCache(NSString *dirName)
{
NSString *imageDir = pathInCacheDirectory(dirName);
BOOL isDir = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL existed = [fileManager fileExistsAtPath:imageDir isDirectory:&isDir];
bool isDeleted = false;
if ( isDir == YES && existed == YES )
{
isDeleted = [fileManager removeItemAtPath:imageDir error:nil];
}
return isDeleted;
}
// save Image to the caches directory
bool saveImageToCacheDir(NSString *directoryPath, UIImage *image, NSString *imageName, NSString *imageType)
{
BOOL isDir = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL existed = [fileManager fileExistsAtPath:directoryPath isDirectory:&isDir];
bool isSaved = false;
if ( isDir == YES && existed == YES )
{
if ([[imageType lowercaseString] isEqualToString:@"png"])
{
isSaved = [UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];
}
else if ([[imageType lowercaseString] isEqualToString:@"jpg"] || [[imageType lowercaseString] isEqualToString:@"jpeg"])
{
isSaved = [UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
}
else
{
NSLog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", imageType);
}
}
return isSaved;
}
// load Image from caches dir to imageview
NSData* loadImageData(NSString *directoryPath, NSString *imageName)
{
BOOL isDir = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL dirExisted = [fileManager fileExistsAtPath:directoryPath isDirectory:&isDir];
if ( isDir == YES && dirExisted == YES )
{
NSString *imagePath = [directoryPath stringByAppendingString : imageName];
BOOL fileExisted = [fileManager fileExistsAtPath:imagePath];
if (!fileExisted) {
return NULL;
}
NSData *imageData = [NSData dataWithContentsOfFile : imagePath];
return imageData;
}
else
{
return NULL;
}
}
[html] view
plaincopy
// create directory in the caches directory
bool createDirInCache(NSString *dirName)
{
NSString *imageDir = pathInCacheDirectory(dirName);
BOOL isDir = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL existed = [fileManager fileExistsAtPath:imageDir isDirectory:&isDir];
bool isCreated = false;
if ( !(isDir == YES && existed == YES) )
{
isCreated = [fileManager createDirectoryAtPath:imageDir withIntermediateDirectories:YES attributes:nil error:nil];
}
return isCreated;
}
// delete directory in the caches directory
bool deleteDirInCache(NSString *dirName)
{
NSString *imageDir = pathInCacheDirectory(dirName);
BOOL isDir = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL existed = [fileManager fileExistsAtPath:imageDir isDirectory:&isDir];
bool isDeleted = false;
if ( isDir == YES && existed == YES )
{
isDeleted = [fileManager removeItemAtPath:imageDir error:nil];
}
return isDeleted;
}
// save Image to the caches directory
bool saveImageToCacheDir(NSString *directoryPath, UIImage *image, NSString *imageName, NSString *imageType)
{
BOOL isDir = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL existed = [fileManager fileExistsAtPath:directoryPath isDirectory:&isDir];
bool isSaved = false;
if ( isDir == YES && existed == YES )
{
if ([[imageType lowercaseString] isEqualToString:@"png"])
{
isSaved = [UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];
}
else if ([[imageType lowercaseString] isEqualToString:@"jpg"] || [[imageType lowercaseString] isEqualToString:@"jpeg"])
{
isSaved = [UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
}
else
{
NSLog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", imageType);
}
}
return isSaved;
}
// load Image from caches dir to imageview
NSData* loadImageData(NSString *directoryPath, NSString *imageName)
{
BOOL isDir = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL dirExisted = [fileManager fileExistsAtPath:directoryPath isDirectory:&isDir];
if ( isDir == YES && dirExisted == YES )
{
NSString *imagePath = [directoryPath stringByAppendingString : imageName];
BOOL fileExisted = [fileManager fileExistsAtPath:imagePath];
if (!fileExisted) {
return NULL;
}
NSData *imageData = [NSData dataWithContentsOfFile : imagePath];
return imageData;
}
else
{
return NULL;
}
}
相关文章推荐
- IOS之文件夹创建、删除,图片在本地的保存和加载
- IOS之文件夹创建、删除,图片在本地的保存和加载
- IOS之文件夹创建、删除,图片在本地的保存和加载
- IOS之文件夹创建、删除,图片在本地的保存和加载
- IOS之文件夹创建、删除,图片在本地的保存和加载
- IOS之文件夹创建、删除,图片在本地的保存和加载
- iOS加载网络图片,删除本地或者网络图片
- IOS在Document目录下创建文件夹、保存、读取、以及删除文件
- ios 下创建,删除文件夹的方法
- android 在Sdcard和本地创建文件保存图片
- iOS下载图片保存在本地
- ios 图片下载并保存到本地
- ios-Swift中如何保存图片到本地
- iOS激情详解之文件夹创建,移动,复制,删除以及归档和反归档
- ios 下创建,删除文件夹的方法
- ios 调用系统相机,图库,相册。将照片保存在相册,保存图片在本地,UIImagePickerController
- 保存远程图片到本地 同时取得第一张图片并创建缩略图
- java13.语言进阶------本地文件操作---文件夹的创建、重命名、删除
- iOS 将图片保存到iPhone本地相册
- ios 加载一张本地图片