在UIScrollView中的特定点检索UIImage的名称

时间:2014-04-22 22:03:30

标签: ios objective-c uiscrollview uiimage

我无法理解这个问题;我在这里寻找答案,但要么没有直接适用于我的问题,要么我只是无法理解它。 (我对此比较陌生,如果有明显的答案,请道歉。)

我正在将一个UIImages数组(包含在UIImageView中)插入到UIScrollView中。 我可以通过编程方式滚动到ScrollView中的点,但我需要能够通过名称识别滚动后当前显示的图像(因此我可以将图像与另一个ScrollView中的图像进行比较)。 < / p>

我是如何创建我的数组并将图像添加到ImageView和ScrollView的。


ViewController.m

-(void)viewDidLoad {

...

// Store the names as strings
stringArr = [[NSMutableArray arrayWithObjects:
                       @"img0",
                       @"img1",
                       @"img2",
                       @"img3",
                       nil] retain];

// Add images to array
dataArr = [[NSMutableArray arrayWithObjects:
    [UIImage imageNamed:[stringArr objectAtIndex:0]],
    [UIImage imageNamed:[stringArr objectAtIndex:1]],
    [UIImage imageNamed:[stringArr objectAtIndex:2]],
    [UIImage imageNamed:[stringArr objectAtIndex:3]],
    nil] retain];

// Use a dictionary to try and make it possible to retrieve an image by name
dataDictionary = [NSMutableDictionary dictionaryWithObjects:dataArr forKeys:stringArr];

i = 0;
currentY = 0.0f;

// Set up contents of scrollview
// I'm adding each of the four images four times, in a random order
for (imageCount = 0; imageCount < 4; imageCount++) {
    // Add images from the array to image views inside the scroll view.
    for (UIImage *image in reelDictionary)
    {
        int rand = arc4random_uniform(4);

        UIImage *images = [dataArr objectAtIndex:rand];
        imgView = [[UIImageView alloc] initWithImage:images];
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        imgView.clipsToBounds = YES;

        // I have tried to use this to tag each individual image
        imgView.tag = i;
        i++;

        CGRect rect = imgView.frame;
        rect.origin.y = currentY;
        imgView.frame = rect;
        currentY += imgView.frame.size.height;
        [scrollReel1 addSubview:reel1_imgView];
        [reel1_imgView release];
    }
}
scrollReel.contentSize = CGSizeMake(100, currentY);
[self.view addSubview:scrollReel];

...

}

这就是我在ScrollView(currentOffset)中的工作方式,以及我需要检索的图像(symbolNo)。 当我测试时,symbolNo的值是正确的,但我不确定如何使用关于图像名称检索的值。

NSInteger currentOffset = scrollReel.contentOffset.y;
NSInteger symbolNo = (currentOffset / 100) + 1;

提前感谢您的任何帮助。

3 个答案:

答案 0 :(得分:0)

没有办法做到这一点。一旦加载了UIImage对象,它就不会存储它的名字。

如果您的所有图片都有数字名称,则可以使用图片视图上的tag属性解决此问题。

否则,您需要找到一种新方法来建模数据。

答案 1 :(得分:0)

你基本上需要反向映射你拥有的东西。这是一个快速而肮脏的解决方案

NSMutableDictionary *indexToImageMap = [NSMutableDictionary new];
for (imageCount = 0; imageCount < 4; imageCount++) {
// Add images from the array to image views inside the scroll view.
for (UIImage *image in reelDictionary)
{
    int rand = arc4random_uniform(4);

    UIImage *images = [dataArr objectAtIndex:rand];
    imgView = [[UIImageView alloc] initWithImage:images];
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    imgView.clipsToBounds = YES;

    // I have tried to use this to tag each individual image
    imgView.tag = i;
    i++;
    [indexToImageMap setObject:imgView forKey:[NSNumber numberWithInt:i];

    CGRect rect = imgView.frame;
    rect.origin.y = currentY;
    imgView.frame = rect;
    currentY += imgView.frame.size.height;
    [scrollReel1 addSubview:reel1_imgView];
    [reel1_imgView release];
}

}

要查找它,

NSInteger currentOffset = scrollReel.contentOffset.y;
NSInteger symbolNo = (currentOffset / 100) + 1;
NSImage *image = [indexToImageMap objectForKey:[NSNumber numberWithInt:symbolNo]];

答案 2 :(得分:0)

子类图像视图并添加imageName属性。如果我明白你的要求,这应该有效。

#import <UIKit/UIKit.h>

@interface myImageView : UIImageView
{
    __strong NSString *imageName;
}

@property (strong) NSString *imageName;

@end



#import "myImageView.h"

@implementation myImageView
@synthesize imageName;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
@end

然后使用字典来保存所有内容而不是数组+字典。

myImageView *imgView1 = [[myImageView alloc] init];
[imgView1 setImageName:@"image_name_here"];
[imgView1 setImage:[UIImage imageNamed:@"image_name_here"]];

NSMutableDictionary *dicti = [[NSMutableDictionary alloc] init];
[dicti setObject:imgView1 forKey:@"image_name_here_1"];
[dicti setObject:imgView2 forKey:@"image_name_here_2"];
[dicti setObject:imgView... forKey:@"image_name_here_..."];

当您找到imageView时,您可以在字典中搜索图像。因为你现在知道imageView的名字。