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

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

Related

Pinch zoom not working in UIScrollview for Multiple image in ios7

I want to pinch zoom in uiscrollview image. i added multiple image in uiscrollview
but pinch zoom is not working i want to pinch zoom UIImageview image. i have refer so many reference but still its not working
Thanks in advance
My code:-
for(int i = 0;i<aryImage.count;i++)
{
//Create a uiimageview
imageView =[[UIImageView alloc]init ];
imageView.frame = CGRectMake((280 *i),0.0, 280, 475);
imageView.image = [UIImage imageNamed:#"default.png"];
imageView.tag = i;
imageView.userInteractionEnabled = YES;
[scrollGallery addSubview:imageView];
scrollGallery.minimumZoomScale = 1.0;
scrollGallery.maximumZoomScale = 2.0;
scrollGallery.delegate = self;
[scrollGallery addSubview:imageView];
}
scrollGallery.contentSize = CGSizeMake(280 * aryImage.count, scrollGallery.frame.size.height);
pageControl.numberOfPages = aryImage.count;
pageControlBeingUsed = NO;
pageControl.currentPage = 0;
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return imageView;
}
Try this:
- (void)scrollViewDidZoom:(UIScrollView *)sv
{
UIView* zoomView = [sv.delegate viewForZoomingInScrollView:sv];
CGRect zvf = zoomView.frame;
if(zvf.size.width < sv.bounds.size.width)
{
zvf.origin.x = (sv.bounds.size.width - zvf.size.width) / 2.0;
}
else
{
zvf.origin.x = 0.0;
}
if(zvf.size.height < sv.bounds.size.height)
{
zvf.origin.y = (sv.bounds.size.height - zvf.size.height) / 2.0;
}
else
{
zvf.origin.y = 0.0;
}
zoomView.frame = zvf;
}
You return only one imageView in viewForZooming but you added an array of them. This may be the cause of your problem. Try to replace your code like this:
UIView *containerView = [[UIView alloc] initWithFrame:scrollGallery.frame];
[scrollGallery addSubview:containerView];
scrollGallery.minimumZoomScale = 1.0;
scrollGallery.maximumZoomScale = 2.0;
scrollGallery.delegate = self;
for(int i = 0;i<aryImage.count;i++)
{
//Create a uiimageview
imageView =[[UIImageView alloc]init ];
imageView.frame = CGRectMake((280 *i),0.0, 280, 475);
imageView.image = [UIImage imageNamed:#"default.png"];
imageView.tag = i;
imageView.userInteractionEnabled = YES;
[containerView addSubview:imageView];
}
scrollGallery.contentSize = CGSizeMake(280 * aryImage.count, scrollGallery.frame.size.height);
// then maybe you need to resize containerView's frame
//..........
pageControl.numberOfPages = aryImage.count;
pageControlBeingUsed = NO;
pageControl.currentPage = 0;
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return containerView;
}
Hope it will help.
UPD: also try to disable userInteraction of your imageViews. They may block interaction with scrollGallery.
UPD2: store all imageViews in predefined array. For, example,
NSMutableArray *arr = [[NSMutableArray alloc] init];
for (int i=0; i<aryImage.count; i++) {
[arr addObject:[[UIImageView alloc] init]];
}
Then in your cycle instead of
imageView =[[UIImageView alloc] init];
use
UIImageView *imageView = [arr objectAtIndex:i];
And don't forget to release everything at the end ;)

Pagination in scrollview

_
viewdidload
{
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[self.scrollView addSubview:subview];
[subview release];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = colors.count;
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (!pageControlBeingUsed) {
// Switch the indicator 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;
}
}
i am using this code for displaying pagination in scrollview with different colors,for identification,i just want to replace it with images instead of colors.i am using this code
NSArray *colors = [NSArray arrayWithObjects:[UIImage imageNamed:#"h1#2x"], [UIImage imageNamed:#"h2#2x"], [UIImage imageNamed:#"h2.1#2x"], nil];
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIImageView*subview = [[UIView alloc] initWithFrame:frame];
UIImage *imggg = [colors objectAtIndex:i];
[subview setBackgroundColor:[UIColor colorWithPatternImage:imggg]];
[self.scrollView addSubview:subview];
[subview release];
}
But i get only two view, and the image is not size to fit it is delocated.How to set images with the above code.Please help me to solve this problem.
Thanks in advance.
I got the answer by reducing the size of the images in the array to exactly the size of the scrollview. My scrollview size is 320,424, and I slice the size of the images to 320,424. It works perfectly. Thanks.
UIView *subview = [[[UIView alloc] initWithFrame:frame] autorelease];
[subview setBackgroundColor:[colors objectAtIndex:i]];
[[self scrollView] addSubview:subview];
this looks like what your code is trying to do...
If you want images on each page though:
UIImage *img = [UIImage imageNamed:#"nameOfImageFile"];
UIImageView *imageView = [[[UIImageView alloc] initWithImage:img] autorelease];
[[self scrollView] addSubview:imageView];
to have multiple images for each page, just use an array of UIImages for the page.
NSArray *imageNames = [NSArray arrayWithObjects:#"image1Name", #"image2name", #"image3Name", nil];
UIImage *img = [UIImage imageNamed:[imageNames objectAtIndex:pageNumber]];
UIImageView *imageView = [[[UIImageView alloc] initWithImage:img] autorelease];
[[self scrollView] addSubview:imageView];
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
IBOutlet UIScrollView *scrollView;
}
#end
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *colors = [NSArray arrayWithObjects:[UIImage imageNamed:#"chiranjeevi.jpeg"], [UIImage imageNamed:#"AR.jpeg"], [UIImage imageNamed:#"sachin.jpeg"], nil];
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width*colors.count,scrollView.frame.size.height);
for (int i = 0; i <[colors count]; i++)
{
UIImage *imggg = [colors objectAtIndex:i];
CGRect frame;
frame.origin.x =scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = scrollView.frame.size;
UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];
subview.image=imggg;
[scrollView addSubview:subview];
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end

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;

How to catch event inside uiscrollview on image

Hi I have populated uiscrollview with images (code is bellow) but I don't know how to add some event to images inside. I want to double tap on image inside scrollview and get some event. Any direction how to achieve this?
arrayOfImages = [[NSMutableArray alloc] init];
NSString *img;
for (img in imgArray)
{
[arrayOfImages addObject:[UIImage imageNamed:img]];
}
NSLog(#"Array initialization complete...");
scrollView = [[UIScrollView alloc] init];
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
scrollView.directionalLockEnabled = YES;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.delegate = self;
scrollView.backgroundColor = [UIColor blueColor];
scrollView.autoresizesSubviews = YES;
scrollView.frame = CGRectMake(0, 0, 320, 29);
[self.view addSubview:scrollView];
NSLog(#"Scroll View initialization setup complete...");
UIImage *imageToAdd;
int x = 0;
int y = 0;
for (imageToAdd in arrayOfImages)
{
UIImageView *temp = [[UIImageView alloc] initWithImage:imageToAdd];
temp.frame = CGRectMake(x, y, 29, 29);
x += 29;
[scrollView addSubview:temp];
}
NSLog(#"Adding images to outlet complete...");
Add a UITapGestureRecognizer to the UIImageView(s).
Look at this guide for more info.

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