Simple Image Gallery using UIScrollView - ios

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]];
}

Related

Load array of images into slider in IOS

I have used a slider from this in my project.
In this slider they are using static images to pass to the slider and these images are visible in slider. But i have array of images coming from service, i passed this array to the slider, the slider works fine but it does not show images in the slider. I'm confused that why it is not displaying it. My code for slider is,
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSArray *arrayOfImages = [userDefaults objectForKey:#"property_images"];
NSLog(#"GGG %#",arrayOfImages);
//self.imagesData=[[NSArray alloc]init];
self.imagesData = arrayOfImages; //[_Image1 mutableCopy]; //#[#"image1.jpg", #"image2.jpg", #"image3.jpg"];
NSLog(#"Image Array is %#",_imagesData);
for(int i=0; i<self.imagesData.count;i++){
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame) * i, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.scrollView.frame))];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.image = [UIImage imageNamed:[self.imagesData objectAtIndex:i]];
[self.scrollView addSubview:imageView];
}
self.scrollView.delegate = self;
index=0;
// Progammatically init a TAPageControl with a custom dot view.
self.customPageControl2 = [[TAPageControl alloc] initWithFrame:CGRectMake(20,self.scrollView.frame.origin.y+self.scrollView.frame.size.height,self.scrollView.frame.size.width,40)];//CGRectMake(0, CGRectGetMaxY(self.scrollView.frame) - 100, CGRectGetWidth(self.scrollView.frame), 40)
// Example for touch bullet event
self.customPageControl2.delegate = self;
self.customPageControl2.numberOfPages = self.imagesData.count;
self.customPageControl2.dotSize = CGSizeMake(20, 20);
self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame) * self.imagesData.count, CGRectGetHeight(self.scrollView.frame));
[self.view addSubview:self.customPageControl2];
and further functions of slider are this,
-(void)viewDidAppear:(BOOL)animated{
timer=[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:#selector(runImages) userInfo:nil repeats:YES];
}
-(void)viewDidDisappear:(BOOL)animated{
if (timer) {
[timer invalidate];
timer=nil;
}
}
-(void)runImages{
self.customPageControl2.currentPage=index;
if (index==self.imagesData.count-1) {
index=0;
}else{
index++;
}
[self TAPageControl:self.customPageControl2 didSelectPageAtIndex:index];
}
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSInteger pageIndex = scrollView.contentOffset.x / CGRectGetWidth(scrollView.frame);
self.customPageControl2.currentPage = pageIndex;
index=pageIndex;
}
// Example of use of delegate for second scroll view to respond to bullet touch event
- (void)TAPageControl:(TAPageControl *)pageControl didSelectPageAtIndex:
(NSInteger)currentIndex
{
index=currentIndex;
[self.scrollView scrollRectToVisible:CGRectMake(CGRectGetWidth(self.view.frame) * currentIndex, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.scrollView.frame)) animated:YES];
}
Try SDWebImage Library for download image from server.
Objective-C:
#import "UIImageView+WebCache.h"
// imageView.image = [UIImage imageNamed:[self.imagesData objectAtIndex:i]]; comment this line and replace with bottom line
[imageView sd_setImageWithURL:[NSURL URLWithString:[self.imagesData objectAtIndex:i]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
Whole code
for(int i=0; i<self.imagesData.count;i++){
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame) * i, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.scrollView.frame))];
imageView.contentMode = UIViewContentModeScaleAspectFill;
[imageView sd_setImageWithURL:[NSURL URLWithString:[self.imagesData objectAtIndex:i]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
[self.scrollView addSubview:imageView];
}
SDWebImage Source
This is your basic setup for the scrollView:
//data
NSArray * imageArray = #[#"a.jpg",#"b.jpg",#"c.jpg"];
//view
UIScrollView * scrollView = [UIScrollView new];
scrollView.frame = CGRectMake(0, 0, 320, 200);
[self.view addSubview:scrollView];
//add in
float x = 0;
float imageWidth = 320;
for (int n = 0; n < imageArray.count; n++){
UIImageView * iv = [UIImageView new];
iv.frame = CGRectMake(x, 0, imageWidth, 200);
iv.image = [UIImage imageNamed:imageArray[n]];
iv.contentMode = UIViewContentModeScaleAspectFill;
iv.clipsToBounds = true;
[scrollView addSubview:iv];
x+=imageWidth;
}
//update content size
scrollView.contentSize = CGSizeMake(x, 200);

Custom UIView subClass with UIScrollView

I am creating a class, that creates an option bar with multiple scroll views accessible by buttons.
This is how I add the subview with its content:
NSArray *scroll1 = [NSArray arrayWithObjects:#"normal",#"tiltshift",#"zoom",#"normal",#"blur",#"outline",#"coming soon", nil];
NSArray *scroll2 = [NSArray arrayWithObjects:#"Emoji Natur-01",#"Emoji Natur-02",#"Emoji Natur-03",#"Emoji Natur-04",#"Emoji Natur-05",#"Emoji Natur-06",#"Emoji Natur-07",#"Emoji Natur-08",#"Emoji Natur-09",#"Emoji Natur-10",#"Emoji Natur-11",#"Emoji Natur-12", nil];
NSArray *scroll3 = [NSArray arrayWithObjects:#"Linear",#"Parallel",#"Parallel 2",#"Crescendo",#"Dubstep",#"Blackhole",#"coming soon", nil];
NSArray *content = [NSArray arrayWithObjects:scroll1,scroll2,scroll3, nil];
[self.view addSubview:[self.bottomOptionView initWithFrame:self.view.frame withContent:content]];
The Subclass then generate a scrollview for each Array, with the buttons based on the content of the array:
-(void)addScrollViewOfContent:(NSArray*)array{
int viewNumber = array.count;
for (int i = 0 ; i < viewNumber; i++) {
//create the main buttons
NSArray *content = array[i];
UIButton *buttonAccess = [UIButton buttonWithType:UIButtonTypeRoundedRect];
CGFloat buttonEdge = 40;
CGFloat buttonSpace = self.frame.size.width /viewNumber;
int placeOfButton = i+1;
CGFloat buttonAnchor = ((placeOfButton*(buttonSpace)) - (buttonSpace /2+ buttonEdge/2));
buttonAccess.frame = CGRectMake(buttonAnchor, 0 , buttonEdge, buttonEdge);
[buttonAccess setBackgroundColor:[UIColor redColor]];
buttonAccess.tag = i;
[buttonAccess addTarget:self action:#selector(buttonAccess:) forControlEvents:UIControlEventTouchDown];
[self addSubview:buttonAccess];
UIScrollView *ScrollView = [[UIScrollView alloc]init];
ScrollView.frame = CGRectMake(0, 50, self.frame.size.width, self.frame.size.height);
ScrollView.showsHorizontalScrollIndicator = YES;
ScrollView.tag = i;
ScrollView.backgroundColor =[UIColor colorWithRed:0 green:0.0 blue:0.0 alpha:0.0];
ScrollView.indicatorStyle = UIScrollViewDecelerationRateNormal ;
//UIImageView *selector = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"FILTER_SELECT.png"]];
CGFloat btnX = 0;
for (int i = 0 ; i < content.count; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(btnX, 2 , 65, 65);
UIImage *img = [UIImage imageNamed:[content objectAtIndex:i]];
[button setBackgroundImage:img forState:UIControlStateNormal];
[button addTarget:self action:#selector(subButtonTarget:) forControlEvents:UIControlEventTouchDown];
button.tag = i;
[button setTitle:[content objectAtIndex:i] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:#"MyriadPro-Cond" size:15];
button.titleLabel.textColor = [UIColor whiteColor];
button.titleLabel.textAlignment = UIBaselineAdjustmentAlignBaselines ;
button.showsTouchWhenHighlighted = YES;
[ScrollView addSubview:button];
btnX = btnX + 100;
}
btnX = btnX - 80;
ScrollView.contentSize = CGSizeMake(btnX + 50, 80);
ScrollView.hidden = YES;
[self.scrollViewArray addObject:ScrollView];
[self addSubview:ScrollView];
}
This gives me exactly what I want.
Then I detect which button, of which scroll view is pressed to trigger the action.
-(void)subButtonTarget:(UIButton *)sender{
int button = sender.tag;
int scrollview = self.selectedScrollView;
[self.videoEditor subAction:button ofScrollView:scrollview];
}
-(void)buttonAccess:(UIButton *)sender{
// self.selectedScrollView = sender.tag;
for (int i=0; i< self.scrollViewArray.count; i++) {
UIScrollView *scrollview= [self.scrollViewArray objectAtIndex:i];
scrollview.hidden= YES;
}
int scrollIndex = sender.tag;
UIScrollView *scrollview= [self.scrollViewArray objectAtIndex:scrollIndex];
scrollview.hidden = NO;
}
Basically when one of the button is touched, it calls a function in the view controller in which the sublclass was initially called.
The problem is that, on touchevent nothing happens, except for NSLog. Would that be because of the dependency relation of the subClass and the VC that uses it?
You should set this one for each UIScrollView
ScrollView.delaysContentTouches = NO;
And try to subclass UIScrollView with override method
-(BOOL)touchesShouldCancelInContentView:(UIView *)view
{
return YES;
}

scrollView with paging enabled not working properly ? (Objective-C)

i want to create paging scrollView with three UIViews. And than wants to add imageView as a subView to these UIViews. i cant figure it out how to calculate the frame of imageViews.
When i run the code without imageViews it works perfectly fine.
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.delegate = self;
self.scrollView.frame = self.view.frame;
NSArray *colorsArray = [NSArray arrayWithObjects:[UIColor blueColor], [UIColor yellowColor],[UIColor greenColor], nil];
NSArray *imagesArray = [NSArray arrayWithObjects:[UIImage imageNamed:#"123.png"],[UIImage imageNamed:#"13.png"],[UIImage imageNamed:#"12.png"],nil];
for (int i = 0; i < colorsArray.count; i++) {
CGRect frame;
frame.origin.x = self.view.frame.size.width * i;
frame.size = self.view.frame.size;
self.scrollView.pagingEnabled = YES;
UIView *view = [[UIView alloc] initWithFrame:frame];
view.backgroundColor = [colorsArray objectAtIndex:i];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:view.frame];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = [imagesArray objectAtIndex:i];
[view addSubview:imageView];
[self.scrollView addSubview:view];
}
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * colorsArray.count, self.scrollView.frame.size.height);
self.pageControl.numberOfPages = 3;
self.pageControl.currentPage = 0;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat pageWidth = CGRectGetWidth(self.scrollView.bounds);
CGFloat pageFraction = self.scrollView.contentOffset.x / pageWidth;
self.pageControl.currentPage = roundf(pageFraction);
}
The frame for the image view needs to be relative to its superview (i.e. view) not the scroll view. To achieve the paging you desire change:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:view.frame];
to:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, view.frame.size.width, view.frame.size.height)];

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..

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