how to display images horizontal UIscrollView Programmatically - ios

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.

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);

Zooming in UIScrollView - Unable to zoom beyond single image and scroll

I have an array of images loading from the NSURL to scroll in and pinch zoom. I was able to implement the scroll part but when I try to zoom any image beyond the first one, the scroll is stuck and so is the scale on the image. Here is my code:
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 100, SCREEN_WIDTH, self.view.bounds.size.height)];
scrollView.showsHorizontalScrollIndicator = FALSE;
scrollView.maximumZoomScale = 4.0;
scrollView.minimumZoomScale = 1.0;
scrollView.delegate = self;
[self.view addSubview:scrollView];
for (int i=0; i<[listOfImagesZoom count]; i++) {
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(leftMargin, self.scrollView.bounds.origin.y-100, SCREEN_WIDTH, self.view.bounds.size.height-100)];
// [imageView setImage:[UIImage imageNamed:#"img_def.png"]];
NSString *urlImage1 = (NSString*)[listOfImagesZoom objectAtIndex:i];
NSString *productImageURL1 = [urlImage1 stringByReplacingOccurrencesOfString:#"~" withString:#""];
NSString *productImageURL2 = [productImageURL1 stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSString* urlImage = [NSString stringWithFormat:#"http://sagana-ideas.azurewebsites.net%#", productImageURL2];
NSURL *imgURL=[NSURL URLWithString:urlImage];
[imageView setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:#"ex_product.png"]];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[scrollView addSubview:imageView];
[imageView setUserInteractionEnabled:NO];
imageView.tag = i;
leftMargin += SCREEN_WIDTH;
}
[scrollView setContentSize:CGSizeMake(leftMargin, 0)];
[scrollView setPagingEnabled:YES];
scrollView.delegate = self;
I have even implemented the
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imageView;
}
It works if there is only single image to load, but is stuck when there is more than 1 image.
You need a ScrollView to display list of your images. Each image was displayed a scrollView(a subclass scrollview) to perform zoom in/out.
Try to look at this: MWPhotoBrowser
You can create a UIView inside scrollview in which you may add your all images and then return that view from, - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView method.

Add images to UIScrollView horizontally with paging

I am trying to display images in a horizontal row using pages,with each page containing 4 images probably separated by a little space of one line or without space. As the user scrolls horizontally, next four images are displayed in the center of view. But using this code:
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, screenHeight/4, screenWidth, screenHeight/4)];
scroll.showsHorizontalScrollIndicator = YES;
scroll.showsVerticalScrollIndicator=NO;
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 3;
int k=1;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[scroll addSubview:awesomeView];
for (int j=0; j<4; j++) {
NSString *imageName = [NSString stringWithFormat:#"image%d.png", k];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect imageFrame=CGRectMake(0, k*screenHeight/4, screenWidth/5, screenHeight/5);
imageView.frame=imageFrame;
[awesomeView addSubview:imageView];
k++;
}
images are displayed vertically on first page view. Moreover it looks like vertical scrolling is working. What I am missing?
In this line:
CGRect imageFrame=CGRectMake(0, k*screenHeight/4, screenWidth/5, screenHeight/5);
You are messing with frame your imageView (inside UIView - awesomeView).
Your k variable is 1,2,3,4
So i assume that you have 4 imageViews in vertical, and next 'column' with four image on the left, right?
Also you don't set contentSize for your scrollView anywhere so, you might not see the second 'column'

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.

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.

Resources