user1625435
user1625435

Reputation: 153

save image from Scroll view

help please. I have this code that shows me images in 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);

}

Now, I want a UILabel, that will show me, Image name, when I will scroll. Help me please, I can't implement that. Thanks a lot.

Upvotes: 0

Views: 148

Answers (2)

Mat
Mat

Reputation: 7643

In your second for loop you can acces to the indexes of the objects of imgArray and imgNames, so try this:

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

If you want to show the label just when the new image is showed, keep the two arrays as iVars and use UIPageControl to track in wich page of the scrollview you are. (Sample code).

Upvotes: 1

Ryan Poolos
Ryan Poolos

Reputation: 18561

In your loop you could do something like this. Read the apple docs. Search google for how to make a label. Its not hard to learn this stuff if you just try.

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

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

Upvotes: 1

Related Questions