ceate array of image in UIscrollview? - iphone

I want to create array of image instead of one image in below code
- (void)viewDidLoad
{
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
scrollView.contentSize = CGSizeMake(550, 800);
NSString *imageName = [NSString stringWithFormat:#"page-1.jpg" ];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[scrollView addSubview:imageView];
imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[imageView release];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[self.view addSubview:scrollView];
[scrollView release];
[super viewDidLoad];
}
how i create it? and deploy another functionality of scroll view.

I changed your code a bit to add 10 images, instead of 1. Is this what you want?
- (void)viewDidLoad
{
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
int numberOfImages = 10;
CGFloat currentX = 0.0f;
for (int i=1; i <= numberOfImages; i++) {
// create image
NSString *imageName = [NSString stringWithFormat:#"page-%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// put image on correct position
CGRect rect = imageView.frame;
rect.origin.x = currentX;
imageView.frame = rect;
// update currentX
currentX += imageView.frame.size.width;
[scrollView addSubview:imageView];
[imageView release];
}
scrollView.contentSize = CGSizeMake(currentX, 800);
scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[self.view addSubview:scrollView];
[scrollView release];
[super viewDidLoad];
}
Hope it helps!

Related

Array of Images display in UIScrollview and Paging successfully but cannot properly zoom-in images in scrollview

I am working on UIScrollview with ImageArray. Scrolling and paging are working but I not able to zoom each image. My code of image Scrollview is below :-
#define IMAGE_WIDTH 320
#define IMAGE_HEIGHT 360
- (void)viewDidLoad
{
[super viewDidLoad];
// TODO – fill with your photos
NSArray *photos = [[NSArray arrayWithObjects:
[UIImage imageNamed:#"photo1m.jpg"],
[UIImage imageNamed:#"photo2m.jpg"],
[UIImage imageNamed:#"photo3m.jpg"],
[UIImage imageNamed:#"photo4m.jpg"],
nil] retain];
// note that the view contains a UIScrollView in aScrollView
int i=0;
for ( NSString *image in photos )
{
UIImage *images = [photos objectAtIndex:i];
imageView = [[UIImageView alloc] initWithImage:images];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.clipsToBounds = YES;
imageView.frame = CGRectMake( IMAGE_WIDTH * i++, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
[self.scrollViewimages addSubview:imageView];
[imageView release];
}
self.scrollViewimages.contentSize = CGSizeMake(IMAGE_WIDTH*i, IMAGE_HEIGHT);
self.scrollViewimages.delegate = self;
}
Need to help for implementing pinch zoom of every images. Please help !
for ( NSString *image in photos )
{
UIImage *images = [photos objectAtIndex:i];
imageView = [[UIImageView alloc] initWithImage:images];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.clipsToBounds = YES;
imageView.tag = 1;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake( IMAGE_WIDTH * i++, 0, IMAGE_WIDTH, IMAGE_HEIGHT)];
scrollView.delegate = self;
scrollView.maximumZoomScale = 3.0f
imageView.frame = scrollView.bounds;
[scrollView addSubview:imageView];
[imageView release];
[self.scrollViewimages addSubview:scrollView];
}
And Implement the delegate method in UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [scrollView viewWithTag:1];
}
#define IMAGE_FOR_ZOOM_TAG (1)
Call following custom method
-(void) setupProductImageViewContainerUI
{
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
if(self.dimView)
{
for(UIView *subView in self.dimView.subviews)
[subView removeFromSuperview];
[self.dimView removeFromSuperview]; self.dimView = nil;
}
self.dimView = [[UIView alloc] initWithFrame:window.bounds];
self.dimView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.5];
self.dimView.userInteractionEnabled = YES;
[window addSubview:self.dimView];
self.imgContainerView = [[UIView alloc] init];
self.imgContainerView.frame = CGRectMake(0, 0, window.frame.size.width, window.frame.size.height);
self.imgContainerView.backgroundColor = [UIColor whiteColor];
[self.dimView addSubview:self.imgContainerView];
UIScrollView *mainScrollView = [[UIScrollView alloc] initWithFrame:window.bounds];
mainScrollView.pagingEnabled = YES;
mainScrollView.delegate = self;
mainScrollView.showsHorizontalScrollIndicator = NO;
mainScrollView.showsVerticalScrollIndicator = NO;
[self.imgContainerView addSubview:mainScrollView];
CGRect innerScrollFrame = mainScrollView.bounds;
IndexOfSlidingPhoto = 0;
for(int i = 0 ; i < listOfImages.count; i++)
{
UIImage *imgProduct = [GeneralClass getImageForSreenFromScreenTable:#"" orCustomTable:#"" OrDefaultImga:[listOfImages objectAtIndex:i]];
UIImageView *imageForZooming = [[UIImageView alloc] initWithImage:imgProduct];
imageForZooming.frame = CGRectMake(0, 0, window.frame.size.width, window.frame.size.height);
imageForZooming.tag = IMAGE_FOR_ZOOM_TAG;
imageForZooming.contentMode = UIViewContentModeScaleAspectFit;
UIScrollView *pageScrollView = [[UIScrollView alloc] initWithFrame:innerScrollFrame];
pageScrollView.minimumZoomScale = 1.0f;
pageScrollView.maximumZoomScale = 20;
pageScrollView.zoomScale = 1.0f;
pageScrollView.contentSize = imageForZooming.bounds.size;
pageScrollView.delegate = self;
pageScrollView.showsHorizontalScrollIndicator = NO;
pageScrollView.showsVerticalScrollIndicator = NO;
[pageScrollView addSubview:imageForZooming];
[mainScrollView addSubview:pageScrollView];
if (i < listOfImages.count -1)
innerScrollFrame.origin.x += innerScrollFrame.size.width;
mainScrollView.contentSize = CGSizeMake(innerScrollFrame.origin.x + innerScrollFrame.size.width, mainScrollView.bounds.size.height);
[window bringSubviewToFront:self.dimView];
}
#pragma Mark -
#pragma Mark - UIScrollView Delegate Methods
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [scrollView viewWithTag:IMAGE_FOR_ZOOM_TAG];
}

Imagescrollview is showing black screen than images

When clicking on gallery button it shows black screen
UIButton *galleryButton = [UIButton buttonWithType:UIButtonTypeCustom];
[galleryButton addTarget:self action:#selector(ScrollView:) forControlEvents:UIControlEventTouchUpInside];
galleryButton.frame = CGRectMake(0, 0, 30, 30);
UIImage *ime = [UIImage imageNamed:#"photos.png"];
[galleryButton setImage:ime forState:UIControlStateNormal];
UIBarButtonItem *gallerybutton = [[UIBarButtonItem alloc] initWithCustomView:galleryButton];
-(void)ScrollView:(id)sender
{
ImageScrollViewController *imagescrollviewcontroller = [[ImageScrollViewController alloc] init];
[self presentViewController:imagescrollviewcontroller animated:YES completion:NULL];
[imagescrollviewcontroller release];
}
- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
[_imageScrollView setBackgroundColor:[UIColor blackColor]];
[_imageScrollView setCanCancelContentTouches:NO];
_imageScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
_imageScrollView.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
_imageScrollView.scrollEnabled = YES;
_imageScrollView.pagingEnabled = YES;
// load all the images from our bundle and add them to the scroll view
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
NSString *imageName = [NSString stringWithFormat:#"image%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;
[_imageScrollView addSubview:imageView];
//[imageView release];
}
[self layoutScrollImages];
- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [_imageScrollView subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
// set the content size so it can be scrollable
[_imageScrollView setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [_imageScrollView bounds].size.height)];
}
Any idea why it is not showing images but black screen. I am following apple sample code for scrolling images. Changes i have made is that they are showing two subview in scrollview. i am showing only one subview.
Thanks for help.

Add 10 different UIImages in UIScrollView

I am trying to add various UIImages under UIImageView and allow them to scroll with UIScrollView. I am not sure how to add various images under UIImageView and let them scroll.
Below is my code which adds an image on UIImageView and make it scrollable.
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:#"ae.jpg"];
imageView = [[UIImageView alloc]initWithImage:image];
imageView.frame = [[UIScreen mainScreen] bounds];
imageView.contentMode = (UIViewContentModeScaleAspectFit);
imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
imageView.backgroundColor = [UIColor clearColor];
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
scrollView.contentMode = (UIViewContentModeScaleAspectFit);
scrollView.contentSize = CGSizeMake(image.size.width,960);
scrollView.pagingEnabled = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.alwaysBounceVertical = NO;
scrollView.alwaysBounceHorizontal = NO;
scrollView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
scrollView.maximumZoomScale = 2.5;
scrollView.minimumZoomScale = 1;
scrollView.clipsToBounds = YES;
[scrollView addSubview:imageView];
[image release];
[imageView release];
[self.view addSubview:scrollView];
}
The idea is basically simple. Let's assume you want to place 3 images in UIScrollView.
Each of images is 300x300. In this case you'll have scroll view with frame:
scrollView.contentSize = CGSizeMake(image.size.width,900);
For every image you must have it's UIImageView with proper frame:
imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, 300, 300)];
imgView2 = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 300, 300, 300)];
imgView3 = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 600, 300, 300)];
imgView1.image = [UIImage imageNamed:#"ProperName.png"];
...
(pay attention to the yOrigin (2nd value in CGRectMake))
and then as you did:
[scrollView addSubview:imgView1];
[scrollView addSubview:imgView2];
[scrollView addSubview:imgView3];
[imgView1 release];
[imgView2 release];
[imgView3 release];
Of course, it's a brief code, you'll optimize it ;)

how set tag of images in image view?

- (void)viewDidLoad {
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
self.title=#"Evolution";
scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
int numberOfImages = 32;
CGFloat currentX = 0.0f;
for (int i=1; i <= numberOfImages; i++) {
// create image
NSString *imageName = [NSString stringWithFormat:#"page-%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
imageView = [[UIImageView alloc] initWithImage:image];
// put image on correct position
CGRect rect = imageView.frame;
rect.origin.x = currentX;
imageView.frame = rect;
imageView.tag=i;
// update currentX
currentX +=454; //mageView.frame.size.width;
[scrollView addSubview:imageView];
[imageView release];
}
[scrollView addGestureRecognizer:tapGesture];
scrollView.contentSize = CGSizeMake(currentX, 800);
scrollView.pagingEnabled=YES;
scrollView.userInteractionEnabled = YES;
scrollView.maximumZoomScale = 15;
scrollView.minimumZoomScale = 0.5;
scrollView.bounces = NO;
scrollView.bouncesZoom = NO;
scrollView.delegate = self;
scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[self.view addSubview:scrollView];
[scrollView release];
[super viewDidLoad];
}
in above code how set tag for each image? so that i can use that tag to call bigger image from web services as a reference.
imageView.image.tag=i;
OR
[imageView.image setTag:i];
In general we can write like this :
imgview.image.tag=i;

doesn't work correctly. addSubview problem

Help me.
addSubview doesn't work.
I want to add "ContentView" on scrollView.
But "ContentView" doesn't appear on screen.
"ContentView" is UIView.
When I change to self.view=scrollView from [self.view addSubview:scrollView],
addSubview doesn't work.
Please teach me how to add UIView on this screen!!
- (void)viewDidLoad {
[super viewDidLoad];
scrollViewMode = ScrollViewModeNotInitialized;
mode = 1;
imageViewArray = [[NSMutableArray alloc] init];
mainStatic = [[MainStatics alloc] init];
[mainStatic setSetting];
NSString* path = [[NSBundle mainBundle] pathForResource:#"Files" ofType:#"plist"];
NSArray* dataFiles = [NSArray arrayWithContentsOfFile:path];
kNumImages = [dataFiles count];
CGRect frame = [UIScreen mainScreen].applicationFrame;
scrollView = [[touchClass alloc] initWithFrame:frame];
scrollView.delegate = self;
scrollView.maximumZoomScale = 5.0f;
scrollView.minimumZoomScale = 1.0f;
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setDelegate:self];
scrollView.delaysContentTouches=NO;
scrollView.userInteractionEnabled = YES;
[scrollView setCanCancelContentTouches:NO];
NSUInteger i;
for (i=0;i<[dataFiles count];i++)
{
UIImage* image = [UIImage imageNamed:[dataFiles objectAtIndex:i]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.userInteractionEnabled = NO;
CGRect rect = imageView.frame;
rect.size.height = 480;
rect.size.width = 320;
imageView.frame = rect;
imageView.tag = i+1;
[imageViewArray addObject:imageView];
}
self.view = scrollView;
[self setPagingMode];
UIView* contentView;
CGRect scRect = CGRectMake(0, 436, 320, 44);
contentView = [[UIView alloc] initWithFrame:scRect];
[contentView addSubview:toolView];
[self addSubview:contentView];
}
I used image array in following function.
toolView is UIView with UIToolbar.
So I want to add toolbar to screen.
Yes, touchClass is subclass of UIScrollView.
1.I see.I forgot release this.Thank you.
2.OK. I modified this.But "ContentView" didn't apear.
are there another reason?
- (void)setPagingMode {
CGSize pageSize = [self pageSize];
NSUInteger page = 0;
for (UIView *view in imageViewArray){
[scrollView addSubview:view];
view.frame = CGRectMake(pageSize.width * page++, 0, pageSize.width, pageSize.height);
}
scrollView.pagingEnabled = YES;
scrollView.showsVerticalScrollIndicator = scrollView.showsHorizontalScrollIndicator = YES;
scrollView.contentSize = CGSizeMake(pageSize.width * [imageViewArray count], pageSize.height);
scrollView.contentOffset = CGPointMake(pageSize.width * currentPage, 0);
scrollViewMode = ScrollViewModePaging;
}
Source isn't clear.
You never use array with UIImageView. There is no info what is toolView etc...
Please provide more detailed source. Is touchClass a subclass of UIScrollView?
What I noticed in your code:
You don't release image and imageView. All this images imageViews will be leaked. At the end of loop you should add [imageView release]; and [image release];
I don't think that it is good idea to send [self addSubview:contentView]; Try to use [self.view addSubview:contentView];