Bart Van Vulpen
Bart Van Vulpen

Reputation: 13

Save image somewhere and use it in another ViewController (iOS)

I've written some code to make a screenshot of the view. I write that image to the photo library. But the thing is, I want to use that image in an other imageView in another ViewController. How can I save the image somewhere in the app and use it in another ViewController?

My code:

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);

Upvotes: 1

Views: 1048

Answers (3)

abbood
abbood

Reputation: 23548

To cache your images I recommend using the NSCache class first ie

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

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

the good news is that NSCache accepts any object of type id so you can store images, mp3s, whatever you want.

If you would like to use the phone's file directory (which is more expensive to use than NSCache..) I recommend you use the Library/Caches directory instead of the Documents directory.. so building on iDevzilla's answer:

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

Upvotes: 0

iDevzilla
iDevzilla

Reputation: 15558

You first need to save your image on the disk:

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];

And then you can create an UIImageView with the file:

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

Upvotes: 1

Ríomhaire
Ríomhaire

Reputation: 3124

If you are using Storyboards, you could pass it to the next view controller like so; in the prepareForSegue method ( this relieves you from having to save it to disk ):

Note: the name of the segue is set by you in MainStoryboard.storyboard. - mySegue.

kMySegue is just a key for that. i.e. #define kMySegue @"mySegue"

imageInOtherViewController is a UIImage in the other view controller.

// 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];

   }

Upvotes: 0

Related Questions