Get tag of images in scrollview - ios

I have a scrollview created using the following code:
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
imageName = [NSString stringWithFormat:image, i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;
imageView.tag = i; // tag our images for later use when we place them in serial fashion
[bigScrollView addSubview:imageView];
}
How can I get the tag of the image currently in the view?

You can get the tag by getting all the subviews of bigScrollView.
for(UIView *subview in [bigScrollView subviews])
{
if([subview isKindOfClass:[UIImageView class]])
{
NSLog("%d",[subview tag]);
}
}

Related

How to start the photo scroller with the selected image from collectionView in ios

I have used the code
imageSlider = [[UIScrollView alloc]initWithFrame:CGRectMake(5, 60, 700, 700)];
imageSlider.pagingEnabled = YES;
imageSlider.showsHorizontalScrollIndicator =NO;
for (int i = 0; i < [myObject count]; i++) {
CGRect frame;
frame.origin.x = imageSlider.frame.size.width * i;
frame.origin.y = 0;
frame.size = imageSlider.frame.size;
NSManagedObject *con = [myObject objectAtIndex:i] ;
UIImage *Image = [UIImage imageWithData:[con valueForKey:#"image"]];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 700, 700)];
imgView.image = Image;
imgView.frame = frame;
[imageSlider addSubview:imgView];
}
imageSlider.contentSize = CGSizeMake(imageSlider.frame.size.width*([myObject count]), 600);
imageSlider.delegate = self;
[self.view addSubview:imageSlider];
It starts from first image I dono how to start image from the selected image in previous viewController. Thanks in advance..

Scrollview not shwoing images

i have retrived images from document directory and want to display that images in scrollview. the images is coming from the document directory but not displaying in scrollview. i have the below code. I have put scrollview graipically on xib and also creat outlet..
self.scrollView.contentSize = CGSizeMake(320, 136);
self.scrollView.delegate = self;
int X=0;
for (int i = 0; i < [imageFileNames count]; i++)
{
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%d/%d.png",b, i]];
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
[images addObject:img];
NSLog(#"%#",getImagePath);
//imge.image=img;
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(X, 0, 100, 100)] ;
[imageView setImage: [UIImage imageNamed:[NSString stringWithFormat:#"%d.png", i]]];
[self.scrollView addSubview: imageView];
X = X + imageView.frame.size.height+5;
if(X > 320)
self.scrollView.contentSize = CGSizeMake(X, 140);
}
Seems you are trying to create the UIImageView from images that are located in your resources (but they are not there!).
[imageView setImage: [UIImage imageNamed:[NSString stringWithFormat:#"%d.png", i]]];
whats the point of placing the images in a array, if you dont use it?
try this instead:
[imageView setImage: [images objectAtIndex:i]];

Simple Image Gallery using UIScrollView

I am trying to create a very basic image gallery that will contains three images and user will be able to slide over and view the images.
My solution is working using UIColor
NSArray *colors = [NSArray arrayWithObjects:[UIColor blueColor], [UIColor redColor], [UIColor greenColor], nil];
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = 320 * i;
frame.origin.y = 0;
frame.size = CGSizeMake(320, 208);
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[self.slideshow addSubview:subview];
}
self.slideshow.contentSize = CGSizeMake(320 * [colors count], 208);
But images do not displayed with similar solution
NSArray *images = [NSArray arrayWithObjects:[UIImage imageWithContentsOfFile:#"pic_1_3a.jpg"], [UIImage imageWithContentsOfFile:#"pic_1_3b.jpg"], [UIImage imageWithContentsOfFile:#"pic_1_3c.jpg"], nil];
for (int i = 0; i < images.count; i++) {
CGRect frame;
frame.origin.x = 320 * i;
frame.origin.y = 0;
frame.size = CGSizeMake(320, 208);
UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];
subview.image = [images objectAtIndex:i];
[self.slideshow addSubview:subview];
}
self.slideshow.contentSize = CGSizeMake(320 * [images count], 208);
Any idea? The UIScrollView exists inside a UIView.
Try and change
NSArray *images = [NSArray arrayWithObjects:[UIImage imageWithContentsOfFile:#"pic_1_3a.jpg"], [UIImage imageWithContentsOfFile:#"pic_1_3b.jpg"], [UIImage imageWithContentsOfFile:#"pic_1_3c.jpg"], nil];
to
NSArray *images = [NSArray arrayWithObjects:[UIImage imageNamed:#"pic_1_3a.jpg"], [UIImage imageNamed:#"pic_1_3b.jpg"], [UIImage imageNamed:#"pic_1_3c.jpg"], nil];
- (void)viewDidLoad
{
arrImages=[[NSArray alloc]initWithObjects: #"bigbazaar_card.jpg", #"CouponImage.png", #"futureBazaar_card.jpg",#"No_deal.png",#"Reliance_card.jpg",#"wallmart_card.jpg",#"westside_card.jpg", nil];
int x=5;
int y=5;
for (int i=0; i<[arrImages count]; i++) {
NSString *str=[arrImages objectAtIndex:i];
UIImageView *currentimage=[[UIImageView alloc]initWithFrame:CGRectMake(x, y, 150, 220)];
[currentimage setImage:[UIImage imageNamed:str]];
[scroll addSubview:currentimage];
UIButton *btnTag=[[UIButton alloc]initWithFrame:CGRectMake(x, y, 150, 220)];
[btnTag setTitle:#"" forState:UIControlStateNormal];
selectedImageIndex=i;
btnTag.tag=i;
NSLog(#"btntag-->%i",btnTag.tag);
[btnTag addTarget:self action:#selector(BtnShowImage:) forControlEvents:UIControlEventTouchUpInside];
[scroll addSubview:btnTag];
x= x+170;
}
scroll.contentSize = CGSizeMake(170* arrImages.count,scroll.frame.size.height);
}
and you have to declare delegate of UIScrollView in .h file as shown below:
#interface ViewController : UIViewController<UIScrollViewDelegate>
and you have to implement ScrollView's Delegate method if you want to display image on click as shown below:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGPoint contentOffset = scrollView.contentOffset;
contentOffset.x += scrollView.frame.size.width / 2;
NSInteger index = contentOffset.x / scrollView.frame.size.width;
NSString *strTemp=[arrImages objectAtIndex:index];
[imageData setImage:[UIImage imageNamed:strTemp]];
}

Add multiple UIImageview in UIScrollview programmatically

UIView *view = nil;
NSArray *subviews = [scrollView subviews];
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
I had placed one Scrollview, within this scroll view i place 5 UIImageview in XIB file
I have one image and one button, whenever I clicked the button the image should be loaded into all the UIImageviews within the UIScrollview programmatically.
and also I used the following coding
for(UIImageView *addview in Scroll)
{
if([addview isKindOfClass:[UIImageView class]])
{
UIImageView *newImage = (UIImageView *)addview;
newImage.image = [UIImage imageNamed:#"EarP010.jpg"];
}
}
Try this,
NSMutableArray * imagesArray = [[NSMutableArray alloc]init];
for (int imageCount = 0; imageCount < (noOfImages);imageCount++)
{
[imagesArray addObject:[UIImage imageNamed:#"localImagePath%d.png",imageCount]];
}
UIScrollView * scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0,0.0,480.0,960.0)];
scrollview.contentSize = CGSizeMake(scrollview.size.width * (noOfImages),scrollview.frame.size.height);
scrollview.pagingEnabled = YES;
CGFloat xPos = 0.0;
for (UIImage * image in imagesArray) {
#autoreleasepool {
UIImageView * imageview = [[UIImageView alloc]initWithImage:image];
imageview.frame = CGRectMake(xPos, 0.0,scrollview.frame.size.width ,self.scrollView.frame.size.height);
[scrollview addSubview:imageview];
xPos += scrollview.size.width;
}
}
Use this
NSArray* objects = [[NSBundle mainBundle] loadNibNamed:#"your xib name" owner:nil options:nil];
UIView* mainView = [objects objectAtIndex:0];
for (UIView* view in [mainView subviews]) {
if([view isKindOfClass:[UIImageView class]])
{
UIImageView *iview = (UIImageView *)view;
iview.image = [UIImage imageNamed:#"your imagename.png"];
}
}
You could add the image views in a while loop if you know how many image view you need.
UIScrollView *sv = [UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
....
[self.view addSubView:sv];
int i = 0;
while(i < numberOfImages){
i = i + 200;
UIImageView *i = [UIImageView alloc] initWithFrame:CGRectMake(20, i, 200, 100)]
....
[sv addSubView:i];
i++;
}
This is one situation you could use. You will need to know the number of image views you need and what the Y values are on these image views. Much of this code is pseudo code and needs to be filled in.

IOS: add some sub-scrollview in a scrollview, image don't appear

I have this code:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *image = [NSArray arrayWithObjects:[UIImage imageNamed:#"1.png"], [UIImage imageNamed:#"2.png"], [UIImage imageNamed:#"3.png"], nil];
for (int i = 0; i < image.count; i++) {
CGRect frame;
frame.origin.x = scrollV.frame.size.width * i;
frame.origin.y = 0;
frame.size = scrollV.frame.size;
UIScrollView *subScrollView = [[UIScrollView alloc] initWithFrame:frame];
UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];
[subview setImage:[image objectAtIndex:i]];
[subScrollView addSubview:subview];
[scrollV addSubview:subScrollView];
[subview release];
[subScrollView release];
}
scrollV.contentSize = CGSizeMake(scrollV.frame.size.width * image.count, scrollV.frame.size.height);
}
scrollV is the main scrollview and it have "paging enabled"
if I use this code I have my scrollV with paging enabled but I see inside it only "1.png" at first page, I don't see others png, why?
You're settings the frame of subview to the incorrect value. That is making it the same frame as its subScrollView so it's pushing it off to the right. Try this:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *image = [NSArray arrayWithObjects:[UIImage imageNamed:#"1.png"], [UIImage imageNamed:#"2.png"], [UIImage imageNamed:#"3.png"], nil];
for (int i = 0; i < image.count; i++) {
CGRect frame;
frame.origin.x = scrollV.frame.size.width * i;
frame.origin.y = 0;
frame.size = scrollV.frame.size;
UIScrollView *subScrollView = [[UIScrollView alloc] initWithFrame:frame];
UIImageView *subview = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height)];
UIImage *image = [image objectAtIndex:i];
subScrollView.contentSize = image.size;
[subview setImage:image];
[subScrollView addSubview:subview];
[scrollV addSubview:subScrollView];
[subview release];
[subScrollView release];
}
scrollV.contentSize = CGSizeMake(scrollV.frame.size.width * image.count, scrollV.frame.size.height);
}
Edit: Also probably best to set the content size of the inner scrollview. I added code to that effect above.

Resources