// 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];
}
// 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;
}
}
分享到:
相关推荐
unity下载服务器图片保存本地并加载(android和ios),判断本地是否存在,不存在就创建文件.
Unity3D IOS下保存和读取资源(保存到文件夹目录),在Unity中IOS下将资源读取和保存,保存可以保存到文件夹目录
unity 保存相册到手机本地(android和Ios)
教你使用shell脚本快速建立iOS项目内的各个文件夹,只需一条指令。
可以对指定的文件夹进行遍历,输出里面的文件类型。具体的看代码吧。供新人学习。
Unity 保存图片到IOS相册 ,可读写Ios 相册 完整工程。 删除了Library 太大
iOS开发中利用UIWebView加本地Html
ios8及以上
在UIWebView中加载本地图片,http://blog.csdn.net/zhangao0086/article/details/7262192
这个本人封装的用3张imageView就实现的轮播图,里面有三个文件夹,一个加载本地图片的封装,一个是加载网络图片的封装,最后一个是加载网络图片带定时器的封装。本人为每一个封装都留有了一个block块接口,使用其...
ios加载图片内存暴涨解决方法,第一中UIKit-setImage,第二种苹果官方demo提供的分片比例裁剪方式,第三种CATiledLayer 详见Demo
这个本人封装的用2张imageView就实现的轮播图,里面有两个文件夹,一个加载本地图片的封装,一个是加载网络图片的封装。本人为每一个封装都留有了一个block块接口,使用其可以做到与外界其他类配合实现自定义功能,...
IOS WKWebView无法打开本地资源JS/CSS/Image,解决跨域问题 本人亲自编写,可以加载本地网站,如cocos creator发布的网页直接在本地打开 能够正确加载js,css/image等
但是iOS竟然没有现成的支持加载和播放Gif的类。 简单地汇总了一下,大概有以下几种方法: 一、加载本地Gif文件 1、使用UIWebView // 读取gif图片数据 UIWebView *webView = [[UIWebView alloc] initWithFrame:...
ios tableview 异步 加载图片
具体使用方法:https://blog.csdn.net/qq_33608748/article/details/82495758
图片下载和保存
iOS 加载SVG文件,svg地图,点击获取地图上点击的位置
webp网络图片加载/本地图片加载 webp格式gif处理
Unity for iOS 打开相册、相机及保存图片到相册的demo