将图像保存到某个地方并在另一个ViewController(iOS)中使用它

时间:2013-05-24 15:08:30

标签: iphone ios objective-c xcode

我写了一些代码来制作视图的截图。我将该图像写入照片库。但事实是,我想在另一个ViewController中的另一个imageView中使用该图像。如何在应用程序中的某处保存图像并在另一个ViewController中使用它?

我的代码:

UIView* captureView = self.view;

UIGraphicsBeginImageContextWithOptions(captureView.bounds.size, captureView.opaque, 0.0);
[captureView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

CGRect cropRect = CGRectMake(0 ,0 ,640,1136);
UIGraphicsBeginImageContextWithOptions(cropRect.size, captureView.opaque, 1.0f);
[screenshot drawInRect:cropRect];
UIImage * customScreenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(customScreenShot , nil, nil, nil);

3 个答案:

答案 0 :(得分:1)

首先需要将图像保存在磁盘上:

NSString *documentDirectory = 
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) 
                                                                        objectAtIndex:0];
NSString *pngFilePath = [NSString stringWithFormat:@"%@/myImage.png",documentDirectory];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(customScreenShot)];
[imageData writeToFile:pngFilePath atomically:YES];

然后您可以使用以下文件创建UIImageView:

UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage 
imageWithContentsOfFile:pngFilePath]];
[self.view addSubview:myImageView];

答案 1 :(得分:0)

如果您使用的是Storyboard,则可以将其传递给下一个视图控制器;在prepareForSegue方法中(这使您不必将其保存到磁盘):

注意:segue的名称由您在MainStoryboard.storyboard中设置。 - mySegue

kMySegue只是关键。即#define kMySegue @"mySegue"

imageInOtherViewController是另一个视图控制器中的UIImage。

// TheFirstViewController.m

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    id destinationViewController = [segue destinationViewController];

    if ([[segue identifier] isEqualToString:kMySegue]){
        if ([destinationViewController respondsToSelector:@selector(setImageInOtherViewController:)]){
            [destinationViewController setImageInOtherViewController:[UIImage imageNamed:@"myImage.png"]];
            FastCameraViewController *viewController = segue.destinationViewController;
            viewController.delegate = self;
        }   
    } 


    // OtherViewController.h

    @interface OtherViewController : UIViewController 
    {
    }
    @property (nonatomic, strong) IBOutlet UIImageView *otherViewControllerImageView;
    @property (nonatomic, strong) UIImage *imageInOtherViewController;

    // OtherViewController.m
    @implementation OtherViewController
    @synthesize otherViewControllerImageView;
    @synthesize imageInOtherViewController;

    - (void)viewDidLoad{
        [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

        [[self otherViewControllerImageView] setImage:imageInOtherViewController];

   }

答案 2 :(得分:0)

要缓存图片,我建议先使用NSCache课程,即

NSCache* imagesCache = [[NSCache alloc] init];
[imagesCache setName:@"imagesCache"];

[imagesCache setObject:customScreenShot forKey:@"image1"];

好消息是NSCache接受id类型的任何对象,因此您可以存储图像,mp3,无论您想要什么。

如果您想使用手机的文件目录(使用比NSCache更昂贵...),我建议您使用Library/Caches目录而不是Documents目录。所以建立在iDevzilla的answer上:

NSString *documentDirectory =
[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)
 objectAtIndex:0];