我有100张图像存储在数组中并使用swipe gesture.
在双击时,我想将该特定图像存储在NSUserDefault
中。任何人都可以帮助我实现这一目标。
我试过这个
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:@""];
先谢谢。
答案 0 :(得分:3)
在-viewDidLoad
中为您的图片添加UITapGestureRecognizer。
您需要在默认设置为UIImageView
的{{1}}上启用用户互动。
NO
然后添加@selector来处理双击,如下所示:
- (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];
}
答案 1 :(得分:0)
您可以像这样在viewDidLoad中为您的图片添加UITapGestureRecognizer
- (void)viewDidLoad
{
UITapGestureRecognizer *gestureTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
[myImageView addGestureRecognizer:gestureTap];
gestureTap.delegate = self;
gestureTap.numberOfTapsRequired = 2;
[self.imageView addGestureRecognizer:gestureTap];
}
然后在手势的动作中,您可以将图像保存到NSUserDefault,就像这样
-(void)doubleTap2:(UITapGestureRecognizer *)gesture
{
[[NSUserDefaults standardUserDefaults] setValue:self.imageView.image forKey:@"imagesaved"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
答案 2 :(得分:0)
您可以尝试这样:
显示:
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];
}
存储......
-(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];
}