当前位置:首页 > 未命名 > 正文内容

Objective-C UIImage存沙盒与从沙盒读取

9年前 (2017-06-06)未命名
// 保存图片并返回路径
- (NSString *)saveImage:(UIImage *)tempImage
{
    NSString *image_fileType = @"";
    if (UIImagePNGRepresentation(tempImage) == nil) {
        image_fileType = @"JPEG";
    }else{
        image_fileType = @"PNG";
    }
    NSString *imageName = [NSString stringWithFormat:@"/%d.%@",1000000+(arc4random() % 999999999),image_fileType];
    NSData * imageData = UIImagePNGRepresentation(tempImage);
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * documentsDirectory = [paths objectAtIndex:0];
    // Now we get the full path to the file
    NSString * fullPathToFile = [documentsDirectory stringByAppendingString:imageName];
    // and then we write it out
    [imageData writeToFile:fullPathToFile atomically:NO];
    return fullPathToFile;
}

// 根据路径获取对应的图片
-(UIImage *)readImage:(NSString *)fullPathToFile{
    NSString * path = fullPathToFile;
    NSData * data = [NSData dataWithContentsOfFile:path];
    UIImage * image = [UIImage imageWithData:data];
    return image;
}

扫描二维码推送至手机访问。

版权声明:本文由小祥子的博客发布,如需转载请注明出处。

本文地址:http://www.xiaoxiangzi.com/post/45.html

相关文章

人像摄影

人像摄影

 上周日早上本来正在家睡觉,看到QQ群里有妹子求约,于是...地点:云大...

一个人的旅行·大理

一个人的旅行·大理

在结束上一份工作后,想着一个人出去走走吧。以前总是抱怨没时间出去,这下有时间了,但只有我一个人。大理 崇圣寺三塔洱海南诏风情岛充满商业气息的古城就不说了。 ...

测评EVO无人机

在本地的无人机群里了解到EVO无人机,于是在淘宝里买了一台回来玩,但收到货后发现跟想象的不一样。关于APP与遥控器首先在APPStore中没有找到这款无人机的APP,国内各大安卓市场也没有找到,最后在...

一个程序员如何快速赚到一百万

          一个程序员如何快速赚到一百万,说的详细点儿就是:一个固定工作者怎么跳出固有的模式...

马上五一小长假咯!

  前言  明天应该是很爽的一天,因为后天五一,我一直觉得放假的前一天最爽,身处假期里反而没有这么爽。  关于IOS开发  刚才在dangdang上瞧了下,随便一本教材都超过50块钱,一本少说也有3c...

滇池红嘴鸥

滇池红嘴鸥

 拍摄时间:2014年11月8日拍摄地点:昆明·海埂大坝使用器材:佳能70D + 55-250mm...