首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在IOS上从UIView将图像保存到应用程序文档文件夹

在IOS上从UIView将图像保存到应用程序文档文件夹
EN

Stack Overflow用户
提问于 2011-07-26 03:51:10
回答 5查看 97.8K关注 0票数 117

我有一个UIImageView,它允许用户放置和持有图像,直到它可以保存。问题是,我不知道如何实际保存和检索我放置在视图中的图像。

我检索图像并将其放置在UIImageView中,如下所示:

//Get Image 
- (void) getPicture:(id)sender {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = (sender == myPic) ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    [self presentModalViewController:picker animated:YES];
    [picker release];
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage (UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    myPic.image = image;
    [picker dismissModalViewControllerAnimated:YES];
}

它在我的UIImageView中很好地显示了选定的图像,但我不知道如何保存它。我将视图的所有其他部分(主要是UITextfield)保存在核心数据中。我已经找了又找,并尝试了许多人们建议的代码,但要么是我输入的代码不正确,要么是这些建议与我设置代码的方式不一致。很可能是前者。我想使用与在UITextFields中保存文本相同的操作(保存按钮)将图像保存在UIImageView中。下面是我保存UITextField信息的方法:

// Handle Save Button
- (void)save {

    // Get Info From UI
    [self.referringObject setValue:self.myInfo.text forKey:@"myInfo"];

就像我之前说的,我已经尝试了几种方法来让它工作,但无法掌握它。这是我有生以来第一次想对一个没有生命的物体造成身体上的伤害,但我设法克制住了自己。

我希望能够将用户放置到应用程序的documents文件夹中的UIImageView中的图像保存起来,然后能够检索它并将其放置在另一个UIImageView中,以便在用户将该视图推送到堆栈上时显示。任何帮助都是非常感谢的!

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2011-07-26 04:41:26

一切都很好,伙计。不要伤害自己或他人。

您可能不希望将这些图像存储在Core Data中,因为如果数据集变得太大,这可能会影响性能。最好将图像写入文件。

NSData *pngData = UIImagePNGRepresentation(image);

这将提取您捕获的图像的PNG数据。在这里,您可以将其写入一个文件:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory 
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"image.png"]; //Add the file name
[pngData writeToFile:filePath atomically:YES]; //Write the file

稍后阅读它也是一样的。构建路径,就像我们刚才做的那样,然后:

NSData *pngData = [NSData dataWithContentsOfFile:filePath];
UIImage *image = [UIImage imageWithData:pngData];

您可能要做的是创建一个为您创建路径字符串的方法,因为您不希望这些代码到处都是。它可能看起来像这样:

- (NSString *)documentsPathForFileName:(NSString *)name
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  
    NSString *documentsPath = [paths objectAtIndex:0];

    return [documentsPath stringByAppendingPathComponent:name]; 
}

希望这对你有帮助。

票数 343
EN

Stack Overflow用户

发布于 2017-08-24 02:09:42

Swift 3.0版本

let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
        
let img = UIImage(named: "1.jpg")!// Or use whatever way to get the UIImage object
let imgPath = URL(fileURLWithPath: documentDirectoryPath.appendingPathComponent("1.jpg"))// Change extension if you want to save as PNG

do{
    try UIImageJPEGRepresentation(img, 1.0)?.write(to: imgPath, options: .atomic)//Use UIImagePNGRepresentation if you want to save as PNG
}catch let error{
    print(error.localizedDescription)
}
票数 3
EN

Stack Overflow用户

发布于 2018-06-19 15:26:17

带扩展的Swift 4

extension UIImage{

func saveImage(inDir:FileManager.SearchPathDirectory,name:String){
    guard let documentDirectoryPath = FileManager.default.urls(for: inDir, in: .userDomainMask).first else {
        return
    }
    let img = UIImage(named: "\(name).jpg")!

    // Change extension if you want to save as PNG.
    let imgPath = URL(fileURLWithPath: documentDirectoryPath.appendingPathComponent("\(name).jpg").absoluteString)
    do {
        try UIImageJPEGRepresentation(img, 0.5)?.write(to: imgPath, options: .atomic)
    } catch {
        print(error.localizedDescription)
    }
  }
}

使用示例

 image.saveImage(inDir: .documentDirectory, name: "pic")
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6821517

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档