Grid Of image with paging in iphone sdk [closed] - iphone

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to display grid of image in scrollview with paging.All images are from server .
I know to use scrollview with paging.But how can we show images in scrollview with paging.
Can i get idea for how to do this?

Better to use your own logic to arrange the images in scroll view
Create a UiScrollView with paging enabled.. to do this please refer this link
Create a for-loop with adjusting X and Y coordinates of your imageView
From the above link ,these two below method will help you to achieve the functionality
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// Update the page when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
- (IBAction)changePage {
// update the scroll view to the appropriate page
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
}

Here you go:
// an array of uiimageviews (will be easier to deal with than UIImages
NSMutableArray * images = [[NSMutableArray alloc] init];
[images addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.jpg"]]];
[images addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.jpg"]]];
[images addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.jpg"]]];
[images addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.jpg"]]];
[images addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.jpg"]]];
[images addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.jpg"]]];
[images addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.jpg"]]];
[images addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.jpg"]]];
[images addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.jpg"]]];
//we're going to split them into groups of four
NSMutableArray * groupsOfFour = [[NSMutableArray alloc] init];
for(NSInteger i = 0; i < [images count]; i+=4)
{
NSRange theRange;
theRange.location = i;
theRange.length = 4;
if( [images count] - i < 4)
{
theRange.length = [images count] - i;
}
if([images subarrayWithRange:theRange])
[groupsOfFour addObject:[images subarrayWithRange:theRange]];
}
//groupsOfFour will now contain arrays of groups of 4 images
//from that we can work out the content width of out scrollview
float contentWidth = 320*[groupsOfFour count];
//make the scrollview
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,320,480)];
scroll.contentSize = CGSizeMake( contentWidth , 480);
scroll.showsHorizontalScrollIndicator = YES;
scroll.pagingEnabled = YES; //set paging enabled
[self.view addSubview:scroll];
//no we loop through the groups of 4 and add each image we find into a uiview
//this is important because then each group will be separate,
//and so the paging will automatically lock to them
for(NSInteger i = 0; i < [groupsOfFour count]; i++)
{
NSMutableArray * thisGroup = [groupsOfFour objectAtIndex:i];
UIView * groupOfFourView = [[UIView alloc] initWithFrame:CGRectMake(i*320, 0, 320, 480)];
UIImageView*topLeft = [thisGroup objectAtIndex:0];
topLeft.frame = CGRectMake(0, 0, 160, 240);
[groupOfFourView addSubview:topLeft];
//these if statements check that there is an image for this position
//(the last page is likely to have only 1 or 2 images on it)
if([thisGroup count] > 1)
{
UIImageView*topRight = [thisGroup objectAtIndex:1];
topRight.frame = CGRectMake(160, 0, 160, 240);
[groupOfFourView addSubview:topRight];
}
if([thisGroup count] > 2)
{
UIImageView*bottomLeft = [thisGroup objectAtIndex:2];
bottomLeft.frame = CGRectMake(0, 240, 160, 240);
[groupOfFourView addSubview:bottomLeft];
}
if([thisGroup count] > 3)
{
UIImageView*bottomRight = [thisGroup objectAtIndex:3];
bottomRight.frame = CGRectMake(160, 240, 160, 240);
[groupOfFourView addSubview:bottomRight];
}
//finally add the group of four to our scrollview
[scroll addSubview:groupOfFourView];
}

Related

Scrolling of Images using UIScrollView and ImageView in iPhone

I am working one of the app, In that app,there is only one view that has an image on it. I want to be able to swipe left or right and have the first image go out of the view and the second image to come in. The images are different and I want it to look like they are connected. I have used UIScrollView and UIImageView on xib files below is my code which i am using in view did load method. When i run the app, only last image is showing in scrollview.
NSMutableArray *images = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:#"iPad.png"],[UIImage imageNamed:#"iPadMini.png"],[UIImage imageNamed:#"iPhone4.png"],[UIImage imageNamed:#"iPhone5.png"], nil];
for (int i = 0; i < [images count]; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
imgView.frame = frame;
imgView.image = [images objectAtIndex:i];
[scrollView addSubview:imgView];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height);
Try with this block of code...
NSMutableArray *images = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:#"iPad.png"],[UIImage imageNamed:#"iPadMini.png"],[UIImage imageNamed:#"iPhone4.png"],[UIImage imageNamed:#"iPhone5.png"], nil];
for (int i = 0; i < [images count]; i++)
{
UIImageView *imgView = [[UIImageView alloc] init];
imgView.frame = CGRectMake(100*i, 0, 100, self.scrollView.frame.size.height);
imgView.image = [images objectAtIndex:i];
[scrollView addSubview:imgView];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height);
Try this..
NSMutableArray *images = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:#"iPad.png"],[UIImage imageNamed:#"iPadMini.png"],[UIImage imageNamed:#"iPhone4.png"],[UIImage imageNamed:#"iPhone5.png"], nil];
for (int i = 0; i < [images count]; i++) {
UIImageView *imgView = [[UIImageView alloc]init];
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
imgView.frame = frame;
imgView.image = [images objectAtIndex:i];
[scrollView addSubview:imgView];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height);
Hope it helps...

UIScrollView the subviews added but can not slide to them

- (void)viewDidLoad
{
MyData *data = [MyData SharedData];
scrollerView.pagingEnabled = YES;
scrollerView.bounces = YES;
int numberOfViews = [data.placePhotos count];
for (int i = 0; i < numberOfViews; i++) {
CGFloat yOrigin = i * self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
awesomeView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 300 , 320)];
NSString * photourl = [[data.placePhotos objectAtIndex:i] stringByReplacingOccurrencesOfString:#"150x75" withString:#"440x440"];
[imageView setImageWithURL:[NSURL URLWithString:photourl]];
[awesomeView addSubview:imageView];
[scrollerView addSubview:awesomeView];
awesomeView = nil;
imageView =nil;
}
I added the UIScrollView in the .xib file , when i tried to pull the scroll view from left to right it does not move, i have 4 subviews added but why i can not view them ?
you should set the content size the scrollView
[self. scrollerView setContentSize:CGSizeMake(320, 1000)];
Give your width and heigth.
add scrollerView.contentSize = CGSizeMake(yOrigin + self.view.frame.size.width, scrollerView.frame.size.height); after the loop (even though I'm a little bit confused about why the x-origin is called yOrigin in your code...)

How to change the image on UIScrollView

I have a scroll view with multiple images. The images are scrollable.
I am using the following code:
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scrollView.pagingEnabled = YES;
scrollView.delegate = self;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
NSInteger numberOfViews = [arrayImage count];
for (int i = 0; i < numberOfViews; i++)
{
CGFloat yOrigin = i * scrollView.frame.size.width;
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(yOrigin +30, 0, self.view.frame.size.width-60 , self.view.frame.size.height)];
imageView.image = [UIImage imageNamed:[arrayImage objectAtIndex:i]];
[scrollView addSubview:imageView];
[imageView release];
}
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * numberOfViews,0);
[self.view addSubview:scrollView];
[scrollView release]
The application will work with iPad orientations, but the images are not properly scrolled in the landscape mode.
It works fine with portrait mode, but the images merge in landscape mode.
If somebody has any idea, please share....
Try this one...
scrollView.delegate = self;
scrollView.scrollEnabled = YES;
int scrollWidth = 120;
scrollView.contentSize = CGSizeMake(scrollWidth,80);
int xOffset = 0;
imageView.image = [UIImage imageNamed:[imagesName objectAtIndex:0]];
for(int index=0; index < [imagesName count]; index++)
{
UIImageView *img = [[UIImageView alloc] init];
img.bounds = CGRectMake(10, 10, 50, 50);
img.frame = CGRectMake(5+xOffset, 0, 50, 50);
NSLog(#"image: %#",[imagesName objectAtIndex:index]);
img.image = [UIImage imageNamed:[imagesName objectAtIndex:index]];
[images insertObject:img atIndex:index];
scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,110);
[scrollView addSubview:[images objectAtIndex:index]];
xOffset += 70;
}
Set this...
imagesName = [[NSArray alloc]initWithObjects:#"image1.jpg",#"image2.jpg",#"image3.jpg",#"image4.jpg",#"image5.jpg",#"image6.png",#"image7.png",#"image9.png",nil];
images = [[NSMutableArray alloc]init];
You have to set the image frame for that purpose everytime the interface orientation changes or you have to redraw the images again after the change in orientation

Display a serial of UIImageViews using the UIImage objects of a NSArray

I can not find out a easy way. Anyone help? Thanks!
Put your image name into array instead of images, that's much better memory-wise.
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
CGFloat currentOffset = 0;
for (int i = 0; i < [imageArray count]; i++) {
UIImageView *tmpImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[imageArray objectAtIndex:i]]];
currentOffset += tmpImageView.frame.size.width;
//currentOffset += tmpImageView.frame.size.height; // for vertical
tmpImageView.frame = CGRectOffset(tmpImageView.frame, currentOffset, 0);
//tmpImageView.frame = CGRectOffset(tmpImageView.frame, 0, currentOffset); // for vertical
[scrollView addSubview:tmpImageView];
[tmpImageView release];
}
scrollView.contentSize = CGSizeMake(currentOffset, scrollView.frame.size.height);
//scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, currentOffset); // for vertical
Then add that scrollView to shichever view you want and release it.
As you can see this will put all your images one next to other horizontally. If you want to do it vertically, simply uncomment all the commented lines and delete the line before each of those commented lines.
This is how one would solve this problem with iOS4. iOS5 adds an easier way to do this but you'll need to look at the doc since it's still under NDA.
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectZero] autorelease];
imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"one.png"],
[UIImage imageNamed:#"two.png"],
[UIImage imageNamed:#"three.png"],
[UIImage imageNamed:#"four.png"],
nil];

How can i recive Selected Index Page in UIScrollView

I'm a beginner at iPhone development. I set 95 pages with (320, 480) pixels in scrollView content size.
I want to get the selected index when I scroll in the UIScrollView.
(void)viewDidLoad {
[super viewDidLoad];
NSLog(#"Selected page %d",scrollView.contentOffset.x) ;
scrollView.pagingEnabled = YES;
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
NSString *imageName = [NSString stringWithFormat:#"page (%d).png", 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;
NSLog(#"Selected page %d",scrollView.contentOffset.x) ;
[scrollView addSubview:imageView];
[imageView release];
}
[self layoutScrollImages];
}
what do you want to do after getting that selected index number.because you can
check the condition like if sender.tag==5 if you want to use at other place as you have assigned tag to each page or you can implement scrollview with paging.check this
example
https://github.com/ykyuen/ScrollViewWithPaging