调整UIImage的大小并将其保存在我的应用程序文件系统中 [英] resize an UIImage and save it on my app file system

查看:161
本文介绍了调整UIImage的大小并将其保存在我的应用程序文件系统中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调整UIImage的大小并将其保存在我的应用文件系统上,

I need to resize an UIImage and save it on my app file system,

我可以调整图片大小

或只需用较小的cgrect显示它,

or just show it with a smaller cgrect for the frame,

但是如何在我的app文件系统中保存这个新调整大小的图像?

but how can i save this newly resized image on my app file sys?

谢谢!

推荐答案

假设您的图像被称为图像

Assuming your image is called image

//Where the 0 denotes the compression (0 to 1).
NSData *imageData = UIImageJPEGRepresentation(image, 0);

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"MyImageFolder"];

NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder

NSString *fullPath = [dataPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg", imageName]]; //add our image to the path

bool successI = [imageData writeToFile:fullPath atomically:YES];
if (successI) {
    //        NSLog(@"Image saved correctly");
} else
    NSLog(@"Error while saving Image");

忘记提及,你可以改变

NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

将其保存在其他地方。

这篇关于调整UIImage的大小并将其保存在我的应用程序文件系统中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆