0

I have 100 images which are stored in the array and displayed in single imageview randomly using swipe gesture.

On double tapping I want to store that particular image in NSUserDefault. Can anyone please help me in achieving this.

I tried this

randIdx=arc4random()%[FrontsCards count];

NSString *imageName=[FrontsCards objectAtIndex:randIdx];

NSString *fullImageName=[NSString stringWithFormat:@"%@",imageName];

int padding=0;

CGRect imageViewFrame=CGRectMake(scrollView.frame.size.width*m+padding, scrollView.frame.origin.y, scrollView.frame.size.width-2*padding, scrollView.frame.size.height);

ImgView=[[UIImageView alloc]initWithFrame:imageViewFrame];

[ImgView setImage:[UIImage imageNamed:fullImageName]];

[scrollView addSubview:ImgView];

[ImgView setAccessibilityIdentifier:[FrontsCards objectAtIndex:randIdx]];


[[NSUserDefaults standardUserDefaults]setObject:UIImagePNGRepresentation(@"") forKey:@""];          

Thanks in Advance.

Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
Jitendra
  • 5,055
  • 2
  • 22
  • 42
  • check this out http://stackoverflow.com/questions/6587453/filtering-single-and-double-taps – p.balmasov May 20 '13 at 11:02
  • What have you tried? Questions must show some research has been carried out and this shows none also your question is doesn't provide much information to what you actually want. – Popeye May 20 '13 at 11:03
  • you can set tag to each one image. for example image.tag = rndNumb. rndNumb = 1..100 ? – Bob May 20 '13 at 11:04

3 Answers3

3

In your -viewDidLoad add UITapGestureRecognizer to your image. You need to enable user interaction on the UIImageView which in set to NO by default.

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.imgView setUserInteractionEnabled:YES];

    imgArray = [NSArray arrayWithObjects:@"Default.png", @"Default2.png", @"Default3.png", nil];
    NSInteger randomNumber = arc4random() % [imgArray count];

    UITapGestureRecognizer *doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
    NSString *strImgName = [imgArray objectAtIndex:randomNumber];
    [self.imgView setImage:[UIImage imageNamed:strImgName]];
    NSLog(@"Image Name = %@", strImgName);
    [self.imgView setTag:randomNumber];

    doubleTapGestureRecognizer.numberOfTapsRequired = 2;
    [self.imgView addGestureRecognizer:doubleTapGestureRecognizer];
}

And then add the @selector to handle the double tap as follows:

- (void)handleDoubleTap:(UITapGestureRecognizer *)gesture {
    int imgViewTag = [gesture.view tag];
    NSString *strImageName = [imgArray objectAtIndex:imgViewTag];
    NSLog(@"Selected Image = %@",strImageName);
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:strImageName forKey:@"imageName"];
}

Sample Project Dropbox Link

icodebuster
  • 8,890
  • 7
  • 62
  • 65
0

You can add UITapGestureRecognizer to your image in viewDidLoad like this

- (void)viewDidLoad
{

     UITapGestureRecognizer *gestureTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
     [myImageView addGestureRecognizer:gestureTap];
     gestureTap.delegate = self;
     gestureTap.numberOfTapsRequired = 2;

     [self.imageView addGestureRecognizer:gestureTap];
}

And then in gesture's action, you can save image to NSUserDefault like this

 -(void)doubleTap2:(UITapGestureRecognizer *)gesture
 {
     [[NSUserDefaults standardUserDefaults] setValue:self.imageView.image forKey:@"imagesaved"];
     [[NSUserDefaults standardUserDefaults] synchronize];
 }
Ajay Chaudhary
  • 1,993
  • 15
  • 23
0

You can may try like this:

Displaying:

  YourMethod {

    randIdx=arc4random()%[FrontsCards count];

    NSString *imageName=[FrontsCards objectAtIndex:randIdx];

    NSString *fullImageName=[NSString stringWithFormat:@"%@",imageName];

    int padding=0;

    CGRect imageViewFrame=CGRectMake(scrollView.frame.size.width*m+padding, scrollView.frame.origin.y, scrollView.frame.size.width-2*padding, scrollView.frame.size.height);

    ImgView=[[UIImageView alloc]initWithFrame:imageViewFrame];

    [ImgView setImage:[UIImage imageNamed:fullImageName]];
    UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedAction:)];
    tapped.numberOfTapsRequired = 2;
    [ImgView addGestureRecognizer:tapped];
    [ImgView setTag:randIndx];
    [scrollView addSubview:ImgView];

}

Storing ...

  -(void)tappedAction:(UITapGestureRecognizer*)gesture
  {
   // storing in nsuserdefault ...
    int indArr = [sender.view tag];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   NSData *imageData = UIImagePNGRepresentation([sender.view image]);
    NSString *thKey = [NSString stringWithFormat:@"%i", indArr];
    [defaults setObject:imageData forKey:theKey];

}

Bob
  • 1,351
  • 11
  • 28