Jitendra
Jitendra

Reputation: 5081

Want Double Tapped Image

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.

Upvotes: 0

Views: 156

Answers (3)

icodebuster
icodebuster

Reputation: 8964

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

Upvotes: 3

Bunyod
Bunyod

Reputation: 1371

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

}

Upvotes: 0

Ajay Chaudhary
Ajay Chaudhary

Reputation: 1991

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

Upvotes: 0

Related Questions