0

I want to store and retrieve an image that was taken from my avcapturesession. I was able to do this with a button press but I want to have the image appear automatically after it is taken. It works fine with an IBAction press. Here's what I have done. I stored the image to user defaults with this code

UIImage *Capture = [UIImage imageWithData:self.jpegPhotoData];
capturetest = Capture;
NSData *imageData;
imageData = [NSKeyedArchiver archivedDataWithRootObject:Capture];
[[NSUserDefaults standardUserDefaults] setObject:imageData forKey:@"image"];

Then I called the NSUserDefaults with a button press using this code.

-(IBAction)test:(id)sender
{
    NSLog(@"WAS IT PRESSED");
    NSData *imageData1;
    imageData1 = [[NSUserDefaults standardUserDefaults] objectForKey:@"image"];
    capturetest = [NSKeyedUnarchiver unarchiveObjectWithData: imageData1];
    self.imgView.image = capture test;
}

This worked perfect. When I pressed the button it showed the image I just took but how do I just get the image to appear automatically? I've tried putting the button code inside a void statement like -(void)test then after the photo was taken I called [self test]. That didn't work. I even tried to call the button press automatically like this but no luck.

[self performSelector: @selector(test:) withObject:self afterDelay: 0.0];
Md. Ibrahim Hassan
  • 5,359
  • 1
  • 25
  • 45
Marc Matteo
  • 103
  • 2
  • 9
  • What type of object is self.imgView? How is imgView connected to NSUserDefaults? Have you tried stepping through `-test:` in the debugger in the case where it works and where it doesn't work and compared what's different? What value does self.imgView have at those times? From where are you calling `-test:` when you're not calling it from a button? – uliwitness Nov 04 '16 at 09:06

2 Answers2

0
UIImage *Capture = [UIImage imageWithData:self.jpegPhotoData];
globalObject = Capture;

You could do one thing that take one temporary global UIImage object and when you taken picture then you can save image to that object and use whenever you want.

Adnan T.
  • 1,166
  • 1
  • 8
  • 8
0

Write a delegate which return the image to required controller,like

YourImageController.h

@protocol ImageSendingDelegate <NSObject>

 -(void)ServiceResponse :(UIImage *)currentImage;
@end

@interface YourImageController : .......

@property(strong,nonatomic)id <ImageSendingDelegate>IDelegate;

YourImageController.m Where you get image data

UIimage *image = [UIImage imageWithData:self.photoData];

[_IDelegate ServiceResponse:image];

import YourImageController.h in DesireController.h

@interface DesireController : UIViewController<ImageSendingDelegate>

@property (strong,nonatomic)YourImageController *delegateObject;

DesireCOntroller.m

 if(_delegateObject==nil)
 {
 _delegateObject=[[YourImageController alloc]init];

  _delegateObject.IDelegate=self

 }

-(void)ServiceResponse:(UIImage *)currentImage
{

UIimage *IamgeRecived=currentImage;

NSLog(@"\n\n\n\n geting image");

}
Nauman Malik
  • 1,326
  • 9
  • 27