调整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);

将其保存在其他地方。