Load array of images into slider in IOS - 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);

Related

ScrollView for images Objective C (iOS)

I am trying to create a welcome page for my app (with screenshots).
I have taken some samples and made it into a text scroll. I have tried for weeks without any success. Thanks
int numberOfPages = 5;
UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
someScrollView.pagingEnabled = YES;
someScrollView.contentSize = CGSizeMake(numberOfPages * someScrollView.frame.size.width, someScrollView.frame.size.height);
[self.view addSubview:someScrollView];
for (int i = 0; i < numberOfPages; i++) {
UILabel *tmpLabel = [[UILabel alloc] initWithFrame:CGRectMake(i * someScrollView.frame.size.width + 20,
20,
someScrollView.frame.size.width - 40,
20)];
tmpLabel.textAlignment = UITextAlignmentCenter;
tmpLabel.text = [NSString stringWithFormat:#"THIS ACTUALLY WORKS!! %d", i];
[someScrollView addSubview:tmpLabel];
}
Here is my failed attempt to do with images:
int numberOfPages = 5;
UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
someScrollView.pagingEnabled = YES;
someScrollView.contentSize = CGSizeMake(numberOfPages * someScrollView.frame.size.width, someScrollView.frame.size.height);
[self.view addSubview:someScrollView];
for (int i = 0; i < numberOfPages; i++) {
UIImageView *tmpLabel = [[UIImageView alloc] initWithFrame:CGRectMake(i * someScrollView.frame.size.width + 20,
20,
someScrollView.frame.size.width - 40,
20)];
tmpLabel.imageAlignment = UIImageAlignmentCenter;
tmpLabel.text = [NSString stringWithFormat:#"THIS ACTUALLY WORKS!! %d", i];
[someScrollView addSubview:tmpLabel];
}
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:subview1.frame];
if (i == 0) {
imageView1.image = [UIImage imageNamed:#"openingScreen1.png"];
}else if (i == 1){
imageView1.image = [UIImage imageNamed:#"openingScreen2.png"];
}else if (i == 2){
imageView1.image = [UIImage imageNamed:#"openingScreen3.png"];
}else {
imageView1.image = [UIImage imageNamed:#"openingScreen4.png"];
}
I also might need to remove the word "Parse" unless the images above sit on top of it? I cannot locate the word.
try this
UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,
self.view.frame.size.width,
self.view.frame.size.height)];
someScrollView.pagingEnabled = YES;
[someScrollView setAlwaysBounceVertical:NO];
//setup internal views
NSInteger numberOfPages = 5;
for (int i = 0; i < numberOfPages; i++)
{
CGFloat xOrigin = i * self.view.frame.size.width+10;
UILabel *tmpLabel = [[UILabel alloc] initWithFrame:CGRectMake(xOrigin,70,self.view.frame.size.width,25)];
tmpLabel.textAlignment = NSTextAlignmentCenter;
tmpLabel.text = [NSString stringWithFormat:#"THIS ACTUALLY WORKS!! %d", i+1];
[someScrollView addSubview:tmpLabel];
// calculate the width
UIImageView *image = [[UIImageView alloc] initWithFrame:
CGRectMake(xOrigin, tmpLabel.frame.size.height + tmpLabel.frame.origin.y + 10,
self.view.frame.size.width,
self.view.frame.size.height)]; // customize your width, height and y co ordinates
image.image = [UIImage imageNamed:[NSString stringWithFormat:
#"openingScreen%d.jpg", i+1]];
image.contentMode = UIViewContentModeScaleAspectFit;
[someScrollView addSubview:image];
}
//set the scroll view content size
someScrollView.backgroundColor = [UIColor blueColor];
someScrollView.contentSize = CGSizeMake(self.view.frame.size.width *
numberOfPages,
self.view.frame.size.height);
//add the scrollview to this view
[self.view addSubview:someScrollView];
the sample OutPut is
here I attached the sample Project also, please use this
Please change your code to something like
int numberOfPages = 5;
UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
someScrollView.pagingEnabled = YES;
//set the scroll view delegate to self so that we can listen for changes
someScrollView.delegate = self;
someScrollView.contentSize = CGSizeMake(numberOfPages * someScrollView.frame.size.width, someScrollView.frame.size.height);
[self.view addSubview:someScrollView];
for (int i = 1; i <= numberOfPages; i++) {
//set the origin of the sub view
CGFloat myOrigin = i * self.view.frame.size.width;
//get the sub view and set the frame
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(myOrigin, 0, self.view.frame.size.width, someScrollView.frame.size.height)];
imageView1.image = [UIImage imageNamed: [NSString stringWithFormat:#"openingScreen%d.png",i ]];
//add the subview to the scroll view
[someScrollView addSubview:imageView1];
}
Add scroll view Delegate methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
}
//dragging ends, please switch off paging to listen for this event
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *) targetContentOffset NS_AVAILABLE_IOS(5_0)
{
//find the page number you are on
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
NSLog(#"Dragging - You are now on page %i",page);
}

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

how to display images horizontal UIscrollView Programmatically

I did Get images for Url.and then display on SCrollView.My code like this
NSMutableArray *al=[NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
for (NSDictionary *diction in al)
{
NSString *geting =[diction valueForKey:#"dealimage"];
NSLog(#"dealimage is %#",geting);
NSData *getdata=[[NSData alloc]initWithData:[NSData dataFromBase64String:geting]];
NSLog(#"good day %#",getdata);
dataimages=[UIImage imageWithData:getdata];
NSLog(#"dataimages %#",dataimages);
[imagesarray addObject:dataimages];
}
scrollView=[[UIScrollView alloc]init];
scrollView.delegate = self;
scrollView.scrollEnabled = YES;
int scrollWidth = 100;
scrollView.contentSize = CGSizeMake(scrollWidth,100);
scrollView.frame=CGRectMake(0, 0, 320, 100);
scrollView.backgroundColor=[UIColor yellowColor];
[self.view addSubview:scrollView];
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, 160, 110);
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 += 170;
}
my Problem is ScrollView is Display but Images are not add So please tell me what wrong in my code
now i need like this
I know how to get images for Url.now i need display images Horizontal ScrollView So Please give me any idea about images .
your coding is Fine, but you are assign the value of [scrollView addSubview:[images objectAtIndex:index]];, this is not the fine one, I modify your code, as per your result Want
UIScrollView *scrollVie=[[UIScrollView alloc]init];
scrollVie.delegate = self;
scrollVie.scrollEnabled = YES;
int scrollWidth = 100;
//scrollVie.contentSize = CGSizeMake(scrollWidth,100);
scrollVie.frame=CGRectMake(0, 51, self.view.frame.size.width, 100);
scrollVie.backgroundColor=[UIColor redColor];
int xOffset = 0;
for(int index=0; index < [imagesarray count]; index++)
{
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(xOffset,10,160, 110)];
img.image = (UIImage*) [imagesarray objectAtIndex:index];
[scrollVie addSubview:img];
xOffset+=100;
}
[self.view addSubview:scrollVie];
scrollVie.contentSize = CGSizeMake(scrollWidth+xOffset,110);
"official" example created by Apple take a look at the StreetScroller Demo.
For the desired outcome that is shown in the picture, ie, the horizontal scroll,
Use ScrollView instead of tableview.
set the pane of scroll to horizontal and dynamically create imageview inside a loop and insert images inside it
hope it helps :)
Instead of using table view why don't you user UICollectionView with their delegates.
you can refer this - Creating a UICollectionView programmatically
It's best way to achieve your requirement.

Z position of UIImageView to that of UIScrollView

I have both UIScrollView and another UIImageView. I want the UIImageView to come over the UIScrollView. I've tried all the bringSubviewToFront and the insertAtIndex stuff but its not working. Please help me out!
Here is the code -
For UIScrollView:
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 300,
SCREEN_WIDTH, SCREEN_HEIGHT)];
scrollView.showsHorizontalScrollIndicator = FALSE;
[self.view addSubview:scrollView];
__block int tagValue = 1;
__block NSInteger tag = 1;
for (int i=0; i<[listOfImages count]; i++) {
NSDictionary *myDic = [listOfImages objectAtIndex:i];
NSString *urlImage = [myDic objectForKey:#"product_image"];
//NSLog(#"%lu",(unsigned long)[listOfImages count]);
image = [[UIImageView alloc] initWithFrame:CGRectMake(leftMargin, 0, 200, 140)];
// [image setImage:[UIImage imageNamed:#"img_def.png"]];
NSURL *imageURL = [NSURL URLWithString:urlImage];
[image setImageWithURL:imageURL
placeholderImage:[UIImage imageNamed:#"img_def.png"]];
image.tag = tag;
image.contentMode = UIViewContentModeScaleAspectFit;
[scrollView insertSubview:image atIndex:1];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleTap:)];
recognizer.numberOfTapsRequired = 1;
recognizer.numberOfTouchesRequired = 1;
recognizer.delegate = self;
[image addGestureRecognizer:recognizer];
[image setUserInteractionEnabled:YES];
leftMargin += SCREEN_WIDTH;
tagValue += 1;
tag += 1;
}
[scrollView setContentSize:CGSizeMake(leftMargin, 0)];
[scrollView setPagingEnabled:YES];
And the image code that I want to come on top of the scroll view -
UIImage *originalPromo = [UIImage imageNamed:#"promo"];
UIImage *scaledPromo = [UIImage imageWithCGImage:[originalPromo CGImage]
scale:(originalPromo.scale *2.0) orientation:(originalPromo.imageOrientation)];
UIImageView *promo = [[UIImageView alloc] init];
[promo setFrame:CGRectMake(45.5, 300.0,
scaledPromo.size.width, scaledPromo.size.height)];
[promo setImage:scaledPromo];
[self.view insertSubview:promo atIndex:100];
Instead of using insertSubview:atIndex use addSubview:. When a subview is added, it's always added on top of all other subviews already in the parent view.

Fetching Selected Image From An Array to be displayed in another view

I'm trying to fetch the selected image from the array of images.And the selected image should be displayed in new view..
- (void)viewDidLoad{
[super viewDidLoad];
selectedTag = 0;
imagesName = [[NSArray alloc]initWithObjects:#"nature1.jpg",#"nature2.jpg",#"nature3.jpg",#"nature4.jpg",#"nature5.png",#"nature6.jpg",nil];
images = [[NSMutableArray alloc]init];
imageScroll.delegate = self;
imageScroll.scrollEnabled = YES;
int scrollWidth = 768;
imageScroll.contentSize = CGSizeMake(scrollWidth,605);
int xOffset = 0;
for(index=0; index < [imagesName count]; index++)
{
UIButton *img = [UIButton buttonWithType:UIButtonTypeCustom];
img.tag = index;
selectedTag = img.tag;
img.bounds = CGRectMake(10, 10, 50, 50);
img.frame = CGRectMake(50+xOffset, 120, 270, 350);
NSLog(#"image: %#",[imagesName objectAtIndex:index]);
[img setImage:[UIImage imageNamed:[imagesName objectAtIndex:index]] forState:UIControlStateNormal];
[images insertObject:img atIndex:index];
imageScroll.contentSize = CGSizeMake(scrollWidth+xOffset,510);
[imageScroll addSubview:[images objectAtIndex:index]];
xOffset += 370;
[img addTarget:self action:#selector(newView:) forControlEvents:UIControlEventTouchUpInside];
selectedTag = img.tag;
}
}
-(void)newView:(id)sender{
NSLog(#"%#",sender);
int f;
f = [sender tag];
CGRect rec = CGRectMake(0, 0, 250, 250);
[self setView:[[[UIView alloc] initWithFrame:rec] autorelease]];
[[self view] setBackgroundColor:[UIColor grayColor]];
UIImageView *im = [[UIImageView alloc] initWithImage:[imagesName objectAtIndex:f]];
im.bounds = CGRectMake(10, 10, 50, 50);
im.frame = CGRectMake(250, 120, 270, 350);
[[self view] addSubview:im];
}
Can anybody help me to fetch the selected image to another view?? Thanks In Advance..
You are setting string instead of image. Use this piece of code
UIImage *img = [UIImage imageNamed:[imagesName objectAtIndex:f]];
UIImageView *im = [[UIImageView alloc] initWithImage:img];

Resources