0

我有 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:@""];          

提前致谢。

4

3 回答 3

3

在您-viewDidLoad将 UITapGestureRecognizer 添加到您的图像中。您需要UIImageViewNO默认情况下启用用户交互。

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

然后添加@selector 来处理双击,如下所示:

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

示例项目 Dropbox 链接

于 2013-05-20T11:25:27.837 回答
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];

}

于 2013-05-20T11:25:20.133 回答
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];
 }
于 2013-05-20T11:13:59.523 回答