Iphone 调整UIImage的大小并将其保存在我的应用程序文件系统中

Iphone 调整UIImage的大小并将其保存在我的应用程序文件系统中,iphone,ios,ipad,uiimage,image-resizing,Iphone,Ios,Ipad,Uiimage,Image Resizing,我需要调整UIImage的大小并将其保存在我的应用程序文件系统中 我能 或者只是用一个较小的cgrect显示它 但是我如何将这个新调整大小的图像保存到我的应用程序文件sys上呢 谢谢 您可以首先通过以下方式将UIImage转换为NSData UIImageJPEGRepresentation 或 然后将数据写入应用程序中的文档或库文件夹。假设您的图像称为图像 //Where the 0 denotes the compression (0 to 1). NSData *imageData =

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

我能

或者只是用一个较小的cgrect显示它

但是我如何将这个新调整大小的图像保存到我的应用程序文件sys上呢


谢谢

您可以首先通过以下方式将UIImage转换为NSData

UIImageJPEGRepresentation


然后将数据写入应用程序中的文档或库文件夹。

假设您的图像称为图像

//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);
把它存到别的地方

NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);