Zooming image in a scroll view - iphone

I want to zoom an image in a scroll view. I used the below code.
All are working fine, but the image is not zooming. Anyone please
help. I am a beginner in iPhone app development.
I had connected the delegate to files manager and set max zooming to 5
I can see the image. But how can I zoom it.
IBOutlet UIScrollView *scrForImg;
IBOutlet UIImageView *imgForScr;
- (void)viewDidLoad {
[super viewDidLoad];
NSString *imagePath = [NSString stringWithFormat:#"myImage.png"];
imgForScr.image = [UIImage imageNamed:imagePath];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return imgForScr;
}

This is all documented here.
HTH.

The image needs to exist in an UIImageView, then you should be able to pan/zoom.
Is it visible? Or does the scroll view contain nothing at all?

Related

infinite scrolling +iphone

I want to implement a wheel like infinte scrolling Rating tool like the below image.
No i had taken an scrollview and set the orange image in scrollview
- (void)viewWillAppear:(BOOL)animated
{
UIImageView * headerImage = [[UIImageView alloc] init];
headerImage.image = [UIImage imageNamed:#"img_2.png"];
SCrl_Wheel.backgroundColor = [UIColor colorWithPatternImage:headerImage.image];
[SCrl_Wheel setContentSize:CGSizeMake(500,0)];
[SCrl_Wheel setShowsHorizontalScrollIndicator:NO];
Lbl_Rate.text=#"0";
[super viewWillAppear:animated];
}
Apple has an example project featuring infinite scrolling it's called StreetScroller, which demonstrates how a UIScrollView subclass can scroll infinitely in the horizontal direction.
There is also an UIScrollView subclass on Github called BAGPagingScrollView, which is paging & infinite, but it has a few bugs you have to fix on your own, because it's not under active development (especially the goToPage: method leads to problems).
I hope this helps you.
Also check this link for easy implementation they also have a sample code attached :)
http://mobiledevelopertips.com/user-interface/creating-circular-and-infinite-uiscrollviews.html

Attempting to create a UIScrollView inside of a UIScrollView, like photos app

I'm having difficulty though. As soon as i make the content size of the outer scrollview be bigger than it's frame, the image that i'm zooming on it's subview (also a scrollview) is behaving weirdly.
Anybody have any ideas on how to do this? I'm making my own because i have problems with various ones on the internet and i also want it to support video, so i'm making it myself.
FOR ZOOMING inner scrollview you have to right this method.
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
if(scrollView!=Innerscr){
UIImageView *v=(UIImageView*)[scrollView viewWithTag:kTagImageViewInScrollView];
return v;
} else {
return nil;
}
}
and for scrolling images you have to write code in under method
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
if(scrollView==Outerscr){
}
}

Detect Zoomscale in UIWebView

My problem is that i want to detect the zoom scale of an UIWebView, i have tried searching it but did not come out with a proper answer.Any help is appreciated......
Well although the UIWebView doesn't have a zoomScale property, UIScrollView does!
So we just scan it's subView's for the scrollView everything sits in and get it that way.
Here's a little (1 method) category that will allow you to get the scale by calling [webView zoomScale].
UIWebView+zoom.h file
#interface UIWebView (zoom)
-(float)zoomScale;
#end
UIWebView+zoom.m file
#implementation UIWebView (zoom)
-(float)zoomScale{
UIScrollView *webViewContentView;
for (UIView *checkView in [self subviews] ) {
if ([checkView isKindOfClass:[UIScrollView class]]) {
webViewContentView = (UIScrollView*)checkView;
break;
}
}
return webViewContentView.zoomScale;
}
#end
UIScrollView Class Reference
UIWebView's View Hierarchy (Don't rely on it though, always scan the webView to avoid code breaking when apple makes changes to iOS)
NOTE: This code should work but has been written in the reply box so hasn't been tested.

Zooming disturbing drawn UIImage in UIView

I have UIView in which I am drawing UIImage
- (void)drawRect:(CGRect)rect
{
[myImage drawInRect:rect];
}
This UIView is added on UIScrollView and returned as zoomable view i.e
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return iImageBgView;
}
Problem is coming when I do the zoom. Image which is drawn is getting shown pixelet i.e blur or disturbed. can anyone help me on "How to refresh / redraw this image"
I am manually refreshing / redrawing the image by calling
[self setNeedsDisplay];
but no use. seems like it is taking the previous coordinate and draw as per that instead of updated coordinate.
Thanks,
Sagar
This issue has been resolved by removing this UIView and adding it again on scrollview.
This has been done on
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
// Remove Imageview and add to Scrollview again
}

How To Create A Gallery on iOS

I'm starting to develop a simple application for iOS, and this application is a simple gallery of some photo (taken from a website).
The first problem I encountered is how to create the view for the gallery.
The view should be something like this (or the Photo App):
however doing a view this way is problematic, first because it uses fixed dimension, and I think is a bit difficult to implement (for me).
The other way is to use a custom cell within a tableview, like this:
but it is still using fixed dimension.
What's the best way to create a gallery, without using any third part lib (like Three20)?
Thanks for any reply :)
PS. I think that using fixed dimension is bad because of the new iphone 4 (with a different resolution), am I right?
You should check out AQGridView which does exactly what you are trying to achieve. Even if you want to write your own custom code, have a look at the AQGridView source as more than likely you will need to use a UIScrollView as a base.
In case that you want to use third party classes, the next tutorials can be mixed, they worked for me.
Here's a good grid view:
custom image picker like uiimagepicker
And if you want to load them asynchronously, use this:
image lazy loading
Both tutorials are very well described and have source code.
The difference in resolution shouldn't be an issue since iOS, if I recall correctly, scales up UI components and images to the right resolution if it detects that it has a retina display. An aside; remember to start making hi/lo-res versions of your graphics if you intend to support both screen sizes without degradation of quality.
As long as you design things in terms of points instead of pixels (which is the way it's done in XCode 4), iOS will be able to handle scaling for you transparently. On a small screen one point will be one pixel, whereas it will be two pixels on a retina display. This allows it to render things with a crisper look on retina displays. Source
I know this question is old, but I didn't see anyone addressing the issue of fixed widths, so I thought I'd contribute for once.
If you don't want to use a third party library, you should do this in UITableView rows. Because of the way UITableView caches cells, it's relatively lightweight in memory. Certainly more so than a possibly very large UIView inside a UIScrollView. I've done it both ways, and I was much happier with the UITableView.
That said, next time I need to do this? I plan to use AQGridView.
Um, since ios6 came out, the right way to do this is with Collection Views:
Apple Docs on CollectionViews
Also, see the two WWDC 2012 sessions on them:
Introduction to Collection Views
Advanced Collection Views
Sadly, Apple did not include a simple gallery or coverflow layout, but it's pretty easy to make one.
I wrote a tutorial on building a media gallery using a UICollectionView. It populates from the user's photo library. I think it will work perfectly for what you are trying to do.
iPhone Programming Tutorial: Creating An Image Gallery Like Over – Part 1
Hope that helps. Cheers!
I did something very similar to this in a project of my own. I just show some parts of the code here, but if you want to view the full code you can view it on GitHub GitHub Repo
First I made a custom Collection View cell with an ImageView
in CustomCollectionCell.h
#import <UIKit/UIKit.h>
#interface CustomCollectionCell : UICollectionViewCell
#property (nonatomic , retain) UIImageView *imageView;
#end
in CustomCollectionCell.m
#import "CustomCollectionCell.h"
#implementation CustomCollectionCell
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setupImageView];
}
return self;
}
#pragma mark - Create Subviews
- (void)setupImageView {
self.imageView = [[UIImageView alloc] initWithFrame:self.bounds];
self.imageView.autoresizingMask = UIViewAutoresizingNone;//UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self addSubview:self.imageView];
}
#end
Then in the view where you want to have the thumbnails you set up the CollectionView
in ThumbNailViewController.m (snippet)
UICollectionView *collectionViewThumbnails;
in ThumbNailViewController.m (snippet)
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
collectionViewThumbnails=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 50) collectionViewLayout:layout];
if (collectionViewThumbnails && layout)
{
[collectionViewThumbnails setDataSource:self];
[collectionViewThumbnails setDelegate:self];
[collectionViewThumbnails registerClass:[CustomCollectionCell class] forCellWithReuseIdentifier:#"cellIdentifier"];
[collectionViewThumbnails setBackgroundColor:[UIColor blackColor]];
[self.view addSubview:collectionViewThumbnails];
}
Then you have the required methods for the collection views. Here you can set up what you
//Number of items in the collectionview
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [galleryData count];
}
//Set up what each cell in the collectionview will look like
//Here is where you add the thumbnails and the on define what happens when the cell is clicked
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//initialize custom cell for the collectionview
CustomCollectionCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
[cell.imageView setClipsToBounds:YES];
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
//format url to load image from
NSString *url = [NSString stringWithFormat:#"http://andrecphoto.weebly.com/uploads/6/5/5/1/6551078/%#",galleryData[indexPath.item]];
//load thumbnail
[cell.imageView setImageWithURL:[NSURL URLWithString:url]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
//Sets up taprecognizer for each cell. (onlcick)
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[cell addGestureRecognizer:tap];
//sets cell's background color to black
cell.backgroundColor=[UIColor blackColor];
return cell;
}
//Sets size of cells in the collectionview
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(100, 100);
}
//Sets what happens when a cell in the collectionview is selected (onlclicklistener)
- (void)handleTap:(UITapGestureRecognizer *)recognizer {
//gets the cell thats was clicked
CustomCollectionCell *cell_test = (CustomCollectionCell *)recognizer.view;
//gets indexpath of the cell
NSIndexPath *indexPath = [collectionViewThumbnails indexPathForCell:cell_test];
if (isConnectedGal)
{
//sets the image that will be displayed in the photo browser
[photoGallery setInitialPageIndex:indexPath.row];
//pushed photobrowser
[self.navigationController pushViewController:photoGallery animated:YES];
}
}
Hopefully that answers your question.
Here is a very good library called FGallery for iOS
-Supports auto-rotation
-thumbnail View
-zoom
-delete