【问题标题】:How to find image name with imageview? [duplicate]如何使用 imageview 查找图像名称? [复制]
【发布时间】:2011-08-15 22:47:59
【问题描述】:
myImageView = [[UIImageView alloc] initWithImage:[imagearray objectAtIndex:j]];
        [myImageView setUserInteractionEnabled:YES];
        [myImageView setTag:i];

        if (i==0||i==1) {

            [UIImageView beginAnimations:nil context:NULL];
            [UIImageView setAnimationDuration:1.5];
            [UIImageView setAnimationRepeatCount:0];
            myImageView.center=CGPointMake(42+t, 220+y);
           [self.view addSubview:myImageView];
            NSLog(@"my imageviw tag %d",myImageView.tag);
            [myImageView release];
            t=t+10; 
            y=y+10; 
        }

【问题讨论】:

标签: iphone


【解决方案1】:

我仍然没有完全理解你的问题..如果你想从 UIImageView 对象中获取图像文件名是不可能的..

原因是一个 UIImageView 实例不存储图像文件。 它存储一个显示 UIImage 实例。当你从 一个文件,你可以这样做:

UIImage *图片 = [UIImage imageNamed:@"myFile.png"];一旦这 完了,就没有了 引用文件名。 UIImage 实例包含数据,不管 它从哪里得到的。就这样 UIImageView 不可能知道 文件名。

而且,即使你可以,你也会 永远不要从视图中获取文件名信息。 这破坏了 MVC。

无耻抄袭this线程的答案..感谢他

【讨论】:

    【解决方案2】:

    如果图像文件来自 uiimageview,则无法获取名称。但是您可以做一件事 - 当您使用图像名称初始化图像视图时,您可以将该图像名称添加到数组中,然后将该数组索引设置为 uiimageview 对象的标记值。现在,当您想要获取图像文件名时,您可以发现使用该数组,您只需要从 uiimageview 获取标签值并从数组中获取该标签的文件名。

    希望它会有所帮助!

    【讨论】:

      【解决方案3】:

      这段代码会帮助你

      - (NSString *) getFileName:(UIImageView *)imgView{
      
          NSString *imgName = [imgView image].accessibilityIdentifier;
      
          NSLog(@"%@",imgName);
      
          return imgName;
      
      }
      

      将其用作:

      NSString *currentImageName = [self getFileName:MyIImageView];
      

      【讨论】:

        猜你喜欢
        • 2011-07-02
        • 1970-01-01
        • 2013-02-21
        • 2011-01-22
        • 1970-01-01
        • 2022-07-07
        • 2021-12-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多