1
上我需要調整的的UIImage並保存我的應用程序的文件系統上,調整大小的UIImage並保存我的應用程序文件系統
或只是爲一個較小的CGRect顯示它框架,
但我怎麼能保存這個新調整大小的圖像在我的應用程序文件sys?
謝謝!
上我需要調整的的UIImage並保存我的應用程序的文件系統上,調整大小的UIImage並保存我的應用程序文件系統
或只是爲一個較小的CGRect顯示它框架,
但我怎麼能保存這個新調整大小的圖像在我的應用程序文件sys?
謝謝!
假設您的圖像被稱爲圖像
//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到NSData的由
UIImageJPEGRepresentation
或
UIImagePNGRepresentation
然後將數據文件或庫文件夾寫在你的應用程序。