从滚动视图保存图像

时间:2012-12-07 13:56:34

标签: iphone objective-c ios

请帮忙。我有这个代码在scrollview中显示图像。:

- (void)viewDidLoad
{
    [super viewDidLoad];


    NSArray *imgNames = [[NSArray alloc] initWithObjects:@"ip1.jpg", @"ip2.jpg", @"ip3.jpg", @"ip4.jpg", @"ip5.jpg", @"ip6.jpg", @"ip7.jpg", @"ip8.jpg", @"ip9.jpg", @"ip10.jpg",@"ip11.jpg",@"ip12.jpg",@"ip13.jpg",@"ip14.jpg",@"ip15.jpg",@"ip16.jpg",@"ip17.jpg",@"ip18.jpg",@"ip19.jpg",@"ip20.jpg",@"ip21.jpg", nil];


    // Setup the array of UIImageViews
    NSMutableArray *imgArray = [[NSMutableArray alloc] init];
    UIImageView *tempImageView;
    for(NSString *name in imgNames) {
        tempImageView = [[UIImageView alloc] init];
        tempImageView.contentMode = UIViewContentModeScaleAspectFill;
        tempImageView.image = [UIImage imageNamed:name];
        [imgArray addObject:tempImageView];

    }
 CGSize pageSize = scrollViewBack.frame.size; // scrollView is an IBOutlet for our UIScrollView
    NSUInteger page = 0;
    for(UIView *view in imgArray) {
        [scrollViewBack addSubview:view];

        // This is the important line
        view.frame = CGRectMake(pageSize.width * page++ + 40, 0, pageSize.width - 80,  pageSize.height);
 }   



    scrollViewBack.contentSize = CGSizeMake(pageSize.width * [imgArray count], pageSize.height);

}

现在,我想要一个UILabel,它会告诉我,图像名称,当我滚动时。请帮帮我,我无法实现。非常感谢。

2 个答案:

答案 0 :(得分:1)

在你的循环中你可以做这样的事情。阅读apple docs。搜索谷歌如何制作标签。如果你试试,不难学习这些东西。

for(NSString *name in imgNames) {
    // ....

    UILabel *label = [[UILabel alloc] initWithFrame:WHEREYOUWANTIT];
    [label setText:name];
}

答案 1 :(得分:1)

在第二个for循环中,您可以访问imgArrayimgNames对象的索引,请尝试以下操作:

int idx = [imgArray indexOfObject:view];
NSString *strName = [imgNames objectAtIndex:idx];
//create the label
label.text=strName;
//add the label in the scrollView

如果您想在显示新图像时显示标签,请将两个阵列保持为iVars并使用UIPageControl跟踪您所在的滚动视图的页面。 (Sample code)。