UIImagePickerController - 保存和加载图像?

发布于 2024-12-11 01:42:04 字数 217 浏览 0 评论 0原文

关于这个主题的信息似乎很分散,但看起来相当混乱,所以我希望你不介意我问。

我有一个 UIImagePickerController 正在工作,让我可以拍照或从库中选择,然后设置我选择的 UIImageView 来显示拍摄/选择的图像。

但是,如何先保存图像,然后在重新打开应用程序时将其加载回来?另外,我将保存多个图像,因此需要唯一地标识它们。

任何帮助将不胜感激,谢谢。

There seems to be a scatter of info on this topic, but it seems rather a muddle, so I hoe you don't mind me asking.

I have a UIImagePickerController working, letting me either take a photo or pick on from the library and then set a UIImageView of my choice to show that image taken/selected.

However, how can I firstly save the image, then load it back in when the application is reopened? Also, I will be saving multiple images so they need to be uniquely identified.

Any help will be massively appreciated, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残月升风 2024-12-18 01:42:04

我不知道有什么方法可以再次从相册中获取照片。似乎没有办法再次检索它。我们做了一些事情来在内部保存图像。

我们要么将图像转换为 NSData 并将其保存在 CoreData 中,要么将其保存到沙箱中。

这是我们使用的一段代码,这是做几件事之一,我从屏幕截图中获取图像,尽管一旦你有了 UIImage,它是一样的。

    // go get our screenshot
UIImage* screenShot = [self createScreenShotThumbnailWithWidth:200];
    // Save screen shot as a png in the documents directory with the UUID of the IoCDScreen
    // Saving to Sandbox
     [self saveImage:screenShot withName:currentScreen.itemUUID];

    // save image to Photo Album - though you can't get a ref back to it
    UIImageWriteToSavedPhotosAlbum(screenShot, self, nil, nil);

    //convert our screen shot PNG to NSData and store it in a CoreData Managed Object
currentScreen.screenImageDevelopment =  [NSData dataWithData: UIImagePNGRepresentation( screenShot )];

上面使用的辅助函数

    //--------------------------------------------------------------------------------------------------------
    //    saveImage:withName:
    //    Description: Save the Image with the name to the Documents folder
    //
    //--------------------------------------------------------------------------------------------------------

- (void)saveImage:(UIImage *)image withName:(NSString *)name {

        //grab the data from our image
    NSData *data = UIImageJPEGRepresentation(image, 1.0);

        //get a path to the documents Directory
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,  YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

        // Add out name to the end of the path with .PNG
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", name]];

        //Save the file, over write existing if exists. 
    [fileManager createFileAtPath:fullPath contents:data attributes:nil];

}

    //--------------------------------------------------------------------------------------------------------
    //    createScreenShotThumbnailWithWidth
    //    Description: Grab a screen shot and then scale it to the width supplied
    //
    //--------------------------------------------------------------------------------------------------------

-(UIImage *) createScreenShotThumbnailWithWidth:(CGFloat)width{
        // Size of our View
    CGSize size = self.view.bounds.size;

        //First Grab our Screen Shot at Full Resolution
    UIGraphicsBeginImageContext(size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();


        //Calculate the scal ratio of the image with the width supplied.
    CGFloat ratio = 0;
    if (size.width > size.height) {
        ratio = width / size.width;
    } else {
        ratio = width / size.height;
    }

        //Setup our rect to draw the Screen shot into 
    CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height);

        //Scale the screenshot and save it back into itself
    UIGraphicsBeginImageContext(rect.size);
    [screenShot drawInRect:rect];
    screenShot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

        //Send back our screen shot
    return screenShot;

}

I don't know of a way to get the Photo from the Photo Album a Second time. There does not seem to be a way to retrieve it again. We do a few things to save the Image internally.

We either convert the Image to NSData and save it in CoreData, or we save it off to our sandbox.

here is a snip of code that we use, this is doing one of a few things, I'm getting the Image from a ScreenShot, though its the same once you have the UIImage.

    // go get our screenshot
UIImage* screenShot = [self createScreenShotThumbnailWithWidth:200];
    // Save screen shot as a png in the documents directory with the UUID of the IoCDScreen
    // Saving to Sandbox
     [self saveImage:screenShot withName:currentScreen.itemUUID];

    // save image to Photo Album - though you can't get a ref back to it
    UIImageWriteToSavedPhotosAlbum(screenShot, self, nil, nil);

    //convert our screen shot PNG to NSData and store it in a CoreData Managed Object
currentScreen.screenImageDevelopment =  [NSData dataWithData: UIImagePNGRepresentation( screenShot )];

Helper Functions used above

    //--------------------------------------------------------------------------------------------------------
    //    saveImage:withName:
    //    Description: Save the Image with the name to the Documents folder
    //
    //--------------------------------------------------------------------------------------------------------

- (void)saveImage:(UIImage *)image withName:(NSString *)name {

        //grab the data from our image
    NSData *data = UIImageJPEGRepresentation(image, 1.0);

        //get a path to the documents Directory
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,  YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

        // Add out name to the end of the path with .PNG
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", name]];

        //Save the file, over write existing if exists. 
    [fileManager createFileAtPath:fullPath contents:data attributes:nil];

}

    //--------------------------------------------------------------------------------------------------------
    //    createScreenShotThumbnailWithWidth
    //    Description: Grab a screen shot and then scale it to the width supplied
    //
    //--------------------------------------------------------------------------------------------------------

-(UIImage *) createScreenShotThumbnailWithWidth:(CGFloat)width{
        // Size of our View
    CGSize size = self.view.bounds.size;

        //First Grab our Screen Shot at Full Resolution
    UIGraphicsBeginImageContext(size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();


        //Calculate the scal ratio of the image with the width supplied.
    CGFloat ratio = 0;
    if (size.width > size.height) {
        ratio = width / size.width;
    } else {
        ratio = width / size.height;
    }

        //Setup our rect to draw the Screen shot into 
    CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height);

        //Scale the screenshot and save it back into itself
    UIGraphicsBeginImageContext(rect.size);
    [screenShot drawInRect:rect];
    screenShot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

        //Send back our screen shot
    return screenShot;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文