use*_*660 107 iphone objective-c uiimage ios
如果我有一个UIImage来自imagePicker的,我如何将它保存到文档目录中的子文件夹?
Dru*_*erB 128
当然,您可以在应用的文档文件夹中创建子文件夹.你NSFileManager用来做那件事.
您可以使用UIImagePNGRepresentation将图像转换为NSData并将其保存到磁盘.
// Create path.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Image.png"];
// Save image.
[UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];
Run Code Online (Sandbox Code Playgroud)
核心数据与将图像保存到磁盘无关.
Nat*_*bot 26
在Swift 3中:
// Create path.
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let filePath = "\(paths[0])/MyImageName.png"
// Save image.
UIImagePNGRepresentation(image)?.writeToFile(filePath, atomically: true)
Run Code Online (Sandbox Code Playgroud)
das*_*ght 25
您必须将图像的表示形式构建为特定格式(例如,JPEG或PNG),然后调用writeToFile:atomically:表示:
UIImage *image = ...;
NSString *path = ...;
[UIImageJPEGRepresentation(image, 1.0) writeToFile:path atomically:YES];
Run Code Online (Sandbox Code Playgroud)
Ale*_*ian 16
以上内容很有用,但它们没有回答您如何保存在子目录中或从UIImagePicker获取图像的问题.
首先,您必须指定控制器在.m或.h代码文件中实现图像选择器的委托,例如:
@interface CameraViewController () <UIImagePickerControllerDelegate>
@end
Run Code Online (Sandbox Code Playgroud)
然后你实现委托的imagePickerController:didFinishPickingMediaWithInfo:方法,你可以从图像选择器中获取照片并保存它(当然,你可能有另一个处理保存的类/对象,但我只是显示代码在方法内):
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// get the captured image
UIImage *image = (UIImage *)info[UIImagePickerControllerOriginalImage];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *imageSubdirectory = [documentsDirectory stringByAppendingPathComponent:@"MySubfolderName"];
NSString *filePath = [imageSubdirectory stringByAppendingPathComponent:@"MyImageName.png"];
// Convert UIImage object into NSData (a wrapper for a stream of bytes) formatted according to PNG spec
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:filePath atomically:YES];
}
Run Code Online (Sandbox Code Playgroud)
如果要保存为JPEG图像,最后3行将是:
NSString *filePath = [imageSubdirectory stringByAppendingPathComponent:@"MyImageName.jpg"];
// Convert UIImage object into NSData (a wrapper for a stream of bytes) formatted according to JPG spec
NSData *imageData = UIImageJPEGRepresentation(image, 0.85f); // quality level 85%
[imageData writeToFile:filePath atomically:YES];
Run Code Online (Sandbox Code Playgroud)
neo*_*eye 11
extension UIImage {
/// Save PNG in the Documents directory
func save(_ name: String) {
let path: String = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let url = URL(fileURLWithPath: path).appendingPathComponent(name)
try! UIImagePNGRepresentation(self)?.write(to: url)
print("saved image at \(url)")
}
}
// Usage: Saves file in the Documents directory
image.save("climate_model_2017.png")
Run Code Online (Sandbox Code Playgroud)
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:path atomically:YES];
Run Code Online (Sandbox Code Playgroud)
其中path是您要将其写入的文件的名称.
首先你应该得到文档目录
/* create path to cache directory inside the application's Documents directory */
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"fileName"];
Run Code Online (Sandbox Code Playgroud)
然后你应该将照片保存到文件中
NSData *photoData = UIImageJPEGRepresentation(photoImage, 1);
[photoData writeToFile:filePath atomically:YES];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
85228 次 |
| 最近记录: |