Resize uiscrollview after scaling so as to enable paging - ios

I am having a scrollview containing 2 image views in one page only.I am scaling scrollview so as to cover full screen on tapping.Now i want after scaling, paging should be enabled.But then it skip every alternate image view. Any help would be appreciated.`
kNumViews = 10;
vScrollObjectHeight = 230; // half screen
vScrollObjectWidth = 160; // half screen
{
for (i = 1; i <= kNumViews; i++)
{
NSString *imageName = [NSString stringWithFormat:#"imageV%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView sizeToFit];
imageView.layer.borderColor = [UIColor grayColor].CGColor;
imageView.layer.borderWidth = 1.5f;
CGRect rect = imageView.frame;
rect.size.height = vScrollObjHeight;
rect.size.width = vScrollObjWidth;
imageView.frame = rect;
imageView.tag = i;
imageView.userInteractionEnabled = YES;
[scrollView2 addSubview:imageView];
}
[self layoutScrollViews];
self.swipeRecognizerUp = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(scaleMove:)];
[self.swipeRecognizerUp setDelegate:self];
[scrollView2 addGestureRecognizer:self.swipeRecognizerUp];
self.swipeRecognizerDown = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(scaleDown:)];
[self.swipeRecognizerDown setDelegate:self];
[scrollView2 addGestureRecognizer:self.swipeRecognizerDown];
self.swipeRecognizerDown.enabled = NO;
}
-(void)scaleMove:(id)sender {
[[[(UIPanGestureRecognizer*)sender view] layer] removeAllAnimations];
[self.view bringSubviewToFront:[sender view]];
CGPoint velocity = [sender velocityInView:self.view];
if(abs(velocity.y) - abs(velocity.x) > 500 && velocity.y < 0){
[sender view].transform = CGAffineTransformMakeScale(1.0, 1.0);
//[sender view].transform = CGAffineTransformMakeTranslation(0, 230);
[UIView animateWithDuration:0.5
animations:^(){
CGAffineTransform scale = CGAffineTransformMakeScale(2.0, 2.0);
CGAffineTransform translate = CGAffineTransformMakeTranslation(0,-230);
[sender view].transform = CGAffineTransformConcat(translate, scale);
//NSLog(#"%f %f",[sender view].frame.size.width,[sender view].frame.size.height);
scrollView2.pagingEnabled = YES;
}
completion:nil];
self.swipeRecognizerDown.enabled = YES;
self.swipeRecognizerUp.enabled = NO;
}
}
}
`

found answer of it...I have set the width of scrollview half and then subclasses the scrollview so as to override pointInside and then normally scaled scrollview.found answer of it...I have set the width of scrollview half and then subclasses the scrollview so as to override pointInside and then normally scaled scrollview.

Related

How should I keep zoom scale on PinchGestureRecognizer?

I want to make zoom function by PinchGestureRecognizer.
I can scale by this code,but imageView is restored whenever I pinch out.
I want to make the function that If I scale imageView sometimes,it isn't restored every time.
And the code scrollView.minimumZoomScale = 3.0,scrollView.maximumZoomScale = 6.0 don't work.
What should I do?
Here is my code
scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
scrollView.frame = self.view.bounds;
scrollView.backgroundColor = [UIColor blueColor];
scrollView.contentSize = CGSizeMake(imgView.bounds.size.width+100, imgView.bounds.size.height);
scrollView.frame = CGRectMake(0,50,320,500);
scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
scrollView.pagingEnabled = YES;
scrollView.bouncesZoom = YES;
scrollView.minimumZoomScale = 3.0;
scrollView.maximumZoomScale = 6.0;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
[self.view addSubview:scrollView];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePinchGesture:)];
[scrollView addGestureRecognizer:pinchGesture];
img =[UIImage imageNamed:[NSString stringWithFormat:#"a.jpg"]];
imgView = [[UIImageView alloc]initWithImage:img];
imgView.frame = CGRectMake(0,0, self.view.frame.size.width, 448);
imgView.userInteractionEnabled = YES;
[scrollView imgView];
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)sender {
CGFloat factor = [(UIPinchGestureRecognizer *)sender scale];
imgView.transform = CGAffineTransformMakeScale(factor, factor);
NSLog(#"factor %f",factor);
}
try use CGAffineTransformScale instead like:
imgView.transform = CGAffineTransformScale(imgView.transform, factor, factor);
Try This Code
UIScrollView *scrollview =[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,imageViewHeight)];
scrollview.showsVerticalScrollIndicator=YES;
scrollview.scrollEnabled=YES;
scrollview.maximumZoomScale = 3.0;
scrollview.delegate = self;
scrollview.userInteractionEnabled=YES;
[self.view addSubview:scrollview];`
NOTE: this is just the another way to achieve your requirement
one thing if u are using UIScrollView there is no need to add UIPinchGestureRecognizer to scroll view by default it has pinch gesture.
for zooming there is a delegate methods need to override
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
and
- (CGRect)zoomRectForScrollView:(UIScrollView *)scrollView withScale:(float)scale withCenter:(CGPoint)center
for example
//initilization
scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
scrollView.frame = self.view.bounds;
scrollView.backgroundColor = [UIColor blueColor];
scrollView.contentSize = CGSizeMake(imgView.bounds.size.width+100, imgView.bounds.size.height);
scrollView.frame = CGRectMake(0,50,320,500);
scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
scrollView.pagingEnabled = YES;
scrollView.bouncesZoom = YES;
scrollView.minimumZoomScale = 3.0;
scrollView.maximumZoomScale = 6.0;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.delegate = self; /* add this line */
[self.view addSubview:scrollView];
//this view will be scaled accordingly
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imgView; //return the view which is the subview of scroll view need to be zoomed
}
//it is the rect for the scaling
- (CGRect)zoomRectForScrollView:(UIScrollView *)scrollView withScale:(float)scale withCenter:(CGPoint)center
{
CGRect zoomRect;
zoomRect.size.height = scrollView.frame.size.height / scale;
zoomRect.size.width = scrollView.frame.size.width / scale;
zoomRect.origin.x = center.x - (zoomRect.size.width / 2.0);
zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);
return zoomRect;
}

How can i set image circular

I have done following code but it does not work. It shows me in circle but in crop image format.
I want to fit image to circle.
UIImage * defaultImage = [UIImage imageNamed:#"empty.png"];
self.myImageView = [[UIImageView alloc] initWithImage:defaultImage];
CGRect myFrame = CGRectMake(90.0f, 100.0f, self.myImageView.frame.size.width/1.5,self.myImageView.frame.size.height/1.5);
[self.myImageView setFrame:myFrame];
[self.myImageView setContentMode:UIViewContentModeScaleAspectFit];
self.myImageView.layer.masksToBounds = NO;
self.myImageView.layer.cornerRadius = self.myImageView.frame.size.width/2;
self.myImageView.layer.borderColor = [UIColor blackColor].CGColor;
self.myImageView.clipsToBounds=YES;
self.myImageView.layer.borderWidth = 2.0f;
[self.view addSubview:self.myImageView];
You can use this method
-(void)setRoundedView:(UIImageView *)roundedView toDiameter:
(float)newSize;
{
CGPoint saveCenter = roundedView.center;
CGRect newFrame = CGRectMake(roundedView.frame.origin.x,
roundedView.frame.origin.y, newSize, newSize);
roundedView.frame = newFrame;
roundedView.layer.cornerRadius = newSize / 2.0;
roundedView.center = saveCenter;
}
Now call the method with your imageview
[self setRoundedView:self.myImageView toDiameter:self.myImageView.frame.size.width];
self.myImageView.clipsToBounds = YES;
Hope it helps
In short,
anyView.layer.cornerRadius = anyView.frame.size.width/2;
Make sure that clipsToBound property of that view is enabled.
Set width & height equal to Your imageView, then set cornerRadius of imageView to half of width & height.
[self createRoundUIView:self.userProfileImageView sizeDiameter:self.userProfileImageView.frame.size.width];
And add following code to bellow method,
-(void)createRoundUIView:(UIImageView *)inputView sizeDiameter:(float)diameterSize
{
CGPoint saveCenter = inputView.center;
CGRect frame = CGRectMake(inputView.frame.origin.x, inputView.frame.origin.y, diameterSize, diameterSize);
inputView.frame = frame;
inputView.layer.cornerRadius = diameterSize / 2.0;
inputView.center = saveCenter;
inputView.clipsToBounds = YES;
}

Animating UIScrollView with paging

I am having a scrollView having 2 subviews per screen and thus 10 subviews.Paging is disabled right now. Now i want whenever I swipe up the subviews inside scrollviews, they should fill the entire screen and after then, normal scrolling is applicable having 10 views each on one screen with paging enabled.Any help??
const NSUInteger kNumViews= 10;
const CGFloat vScrollObjWidth = 160.0;
const CGFloat vScrollObjHeight = 280.0;
- (void)layoutScrollViews
{
UIView *view = nil;
NSArray *subviews = [scrollView2 subviews];
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 += (vScrollObjWidth);
}
}
[scrollView2 setContentSize:CGSizeMake((kNumViews+1) * vScrollObjWidth, scrollView2.bounds.size.height)];
}
- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
scrollView2.delegate = self;
[scrollView2 setBackgroundColor:[UIColor blackColor]];
scrollView2.scrollEnabled = YES;
for (i = 1; i <= kNumViews; i++)
{
NSString *imageName = [NSString stringWithFormat:#"imageV%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView sizeToFit];
imageView.layer.borderColor = [UIColor grayColor].CGColor;
imageView.layer.borderWidth = 1.5f;
CGRect rect = imageView.frame;
rect.size.height = vScrollObjHeight;
rect.size.width = vScrollObjWidth;
imageView.frame = rect;
imageView.tag = i;
imageView.userInteractionEnabled = YES;
[scrollView2 addSubview:imageView];
}
[self layoutScrollViews];
self.swipeRecognizerUp = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(scaleMove:)];
[self.swipeRecognizerUp setDelegate:self];
[scrollView2 addGestureRecognizer:self.swipeRecognizerUp];
self.swipeRecognizerDown = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(scaleDown:)];
[self.swipeRecognizerDown setDelegate:self];
[scrollView2 addGestureRecognizer:self.swipeRecognizerDown];
self.swipeRecognizerDown.enabled = NO;
}
-(void)scaleMove:(id)sender {
[[[(UIPanGestureRecognizer*)sender view] layer] removeAllAnimations];
[self.view bringSubviewToFront:[sender view]];
CGPoint velocity = [sender velocityInView:self.view];
if(abs(velocity.y) - abs(velocity.x) > 500 && velocity.y < 0){
CGAffineTransform scale = CGAffineTransformMakeScale(1, 0.82);
CGAffineTransform translate = CGAffineTransformMakeTranslation(0,254);
[sender view].transform = CGAffineTransformConcat(translate, scale);
[UIView animateWithDuration:0.5
animations:^(){
CGAffineTransform scale = CGAffineTransformMakeScale(2, 1.64);
CGAffineTransform translate = CGAffineTransformMakeTranslation(0,-254);
[sender view].transform = CGAffineTransformConcat(translate, scale);
}
completion:nil];
scrollView2.pagingEnabled = YES;
self.swipeRecognizerDown.enabled = YES;
self.swipeRecognizerUp.enabled = NO;
}
}
-(void)scaleDown:(id)sender {
[[[(UIPanGestureRecognizer*)sender view] layer] removeAllAnimations];
[self.view bringSubviewToFront:[sender view]];
CGPoint velocity = [sender velocityInView:self.view];
if(abs(velocity.y) - abs(velocity.x) > 500 && velocity.y > 0){
[sender view].transform = CGAffineTransformMakeScale(2, 1.64);
[UIView animateWithDuration:0.5
animations:^(){
[sender view].transform = CGAffineTransformMakeScale(1.0, 0.82);
}
completion:nil];
scrollView2.pagingEnabled = NO;
self.swipeRecognizerUp.enabled = YES;
self.swipeRecognizerDown.enabled = NO;
}
}

Zooming UIImageView in a UIScrollView acts weirdly

I have the following problem, guys. I have an app that is pretty much like Apple's PhotoScroller. I want to jump from image to image by swiping the screen. I can do that, but I can't zoom the images. Here's the problem - I have an array with images. If the array has only one object inside, there's no problem with zooming. But if there are more images in the array, they acts weirdly when I try to zoom. The image is not being zoomed and it goes where it wants off the screen. Here is my code:
int pageWidth = 1024;
int pageHeight = 768;
int counter = 0;
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, pageWidth, pageHeight)];
CGRect containerFrame = self.view.frame;
scrollView = [[UIScrollView alloc] initWithFrame:containerFrame];
[self.view addSubview:scrollView];
NSMutableArray *all = [[NSMutableArray alloc] init];
[all addObject:[UIImage imageNamed:#"222.jpg"]];
CGSize scrollSize = CGSizeMake(pageWidth * [all count], pageHeight);
[scrollView setContentSize:scrollSize];
for (UIImage *image in all)
{
UIImage *pageImage = [[UIImage alloc] init];
pageImage = [all objectAtIndex:counter];
CGRect scrollFrame = CGRectMake(pageWidth * counter, 0, pageWidth, pageHeight);
miniScrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
[scrollView addSubview:miniScrollView];
CGSize miniScrollSize = CGSizeMake(pageImage.size.width, pageImage.size.height);
[miniScrollView setContentSize:miniScrollSize];
UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, pageImage.size.width, pageImage.size.height)];
tempImageView.image = [all objectAtIndex:counter];
self.imageView = tempImageView;
miniScrollView.maximumZoomScale = 3.0;
miniScrollView.minimumZoomScale = 1.0;
miniScrollView.clipsToBounds = YES;
miniScrollView.delegate = self;
miniScrollView.showsHorizontalScrollIndicator = NO;
miniScrollView.showsVerticalScrollIndicator = NO;
miniScrollView.bouncesZoom = YES;
[miniScrollView addSubview:imageView];
counter ++;
}
[scrollView setPagingEnabled:YES];
[scrollView setScrollEnabled:YES];
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imageView;
}
Do you have any ideas what's wrong? Because I am trying to get this work almost 2 weeks.
I also worked on such sort of App. The first thing that you can do is to take a separate subclass of your ScrollView so that all the paging and zooming operations can be handled easily. In your scrollView Class, You can take reference from the following code snippet.
#interface PhotoScrollView : UIScrollView<UIScrollViewDelegate,UIGestureRecognizerDelegate>
{
int finalPhotoWidth;
int finalPhotoHeight;
}
// to contain image
#property (strong, nonatomic) UIImageView *imageView;
- (id)initWithFrame:(CGRect)frame andImage:(UIImage *)photo
{
self = [super initWithFrame:frame];
if (self)
{
// Initialization code
[self initializeScrollViewWithImage:photo];
//setting gesture recognizer for single tap
UITapGestureRecognizer *singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(scrollViewSingleTapped:)];
singleTapRecognizer.delegate = self;
singleTapRecognizer.numberOfTapsRequired = 1;
[self addGestureRecognizer:singleTapRecognizer];
//setting gesture recognizer for Double tap
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(scrollViewDoubleTapped:)];
doubleTapRecognizer.delegate = self;
doubleTapRecognizer.numberOfTapsRequired = 2;
[self addGestureRecognizer:doubleTapRecognizer];
[singleTapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];
singleTapActivated = FALSE;
self.backgroundColor = [UIColor blackColor];
self.minimumZoomScale = 1.0f;
self.maximumZoomScale = 15.0f;
self.zoomScale = 1.0f;
self.delegate = self;
}
return self;
}
//for sizing the frame by giving height and width
-(void)initializeScrollViewWithImage:(UIImage*)photo
{
finalPhotoWidth = photo.size.width;
finalPhotoHeight = photo.size.height;
//Pre-checking of frame and setting the height and width accordingly
if (finalPhotoHeight > self.frame.size.height)
{
// if photo height is bigger than frame height, re-adjust the photo height and width accordingly
finalPhotoHeight = self.frame.size.height;
finalPhotoWidth = (photo.size.width/photo.size.height) * finalPhotoHeight;
}
if (finalPhotoWidth > self.frame.size.width)
{
// if photo width is bigger than frame width, re-adjust the photo height and width accordingly
finalPhotoWidth = self.frame.size.width;
finalPhotoHeight = (photo.size.height/photo.size.width) * finalPhotoWidth;
}
if (finalPhotoHeight < self.frame.size.height && finalPhotoWidth < self.frame.size.width)
{
// if the photo is smaller than frame, increase the photo width and height accordingly
finalPhotoWidth = self.frame.size.width;
finalPhotoHeight = self.frame.size.height;
}
//initialising imageView with the thumbnail photo
self.imageView = [[UIImageView alloc] initWithImage:photo];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
//setting frame according to the modified width and height
if(!isnan(finalPhotoWidth) && !isnan(finalPhotoHeight))
{
self.imageView.frame = CGRectMake( (self.frame.size.width - finalPhotoWidth) / 2, (self.frame.size.height - finalPhotoHeight)/2, finalPhotoWidth, finalPhotoHeight);
}
// setting scrollView properties
self.contentSize = self.imageView.frame.size;
// add image view to scroll view
[self addSubview:self.imageView];
}
//to deny the simultaneous working of single and double taps
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return NO;
}
// Gesture handleer for single tap gesture
-(void)scrollViewSingleTapped:(UITapGestureRecognizer *)recognizer
{
if(!singleTapActivated)
{
//do as per requirement
singleTapActivated = TRUE;
}
else
{
//do as per requirement
singleTapActivated = FALSE;
}
}
//for zooming after double tapping
- (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer
{
//to check whether image is zoomed
if (self.zoomScale != 1.0f)
{
//if image is zoomed, then zoom out
[self setZoomScale:1.0 animated:YES];
self.imageView.frame = CGRectMake( (self.frame.size.width - finalPhotoWidth) / 2, (self.frame.size.height - finalPhotoHeight)/2, finalPhotoWidth, finalPhotoHeight);
[self.observer photoZoomStopped];
}
else
{
// get the point of image which is double tapped
CGPoint pointInView = [recognizer locationInView:self.imageView];
// Get a zoom scale that's zoomed in slightly, capped at the maximum zoom scale specified by the scroll view
CGFloat newZoomScale = self.zoomScale * 4.0f;
newZoomScale = MIN(newZoomScale, self.maximumZoomScale);
// Figure out the rect we want to zoom to, then zoom to it
CGSize scrollViewSize = self.imageView.frame.size;
CGFloat w = scrollViewSize.width / newZoomScale;
CGFloat h = scrollViewSize.height / newZoomScale;
CGFloat x = pointInView.x - (w / 2.0f);
CGFloat y = pointInView.y - (h / 2.0f);
CGRect rectToZoomTo = CGRectMake(x, y, w, h);
[self zoomToRect:rectToZoomTo animated:YES];
}
}
// To re-center the image after zooming in and zooming out
- (void)centerScrollViewContents
{
CGSize boundsSize = self.bounds.size;
CGRect contentsFrame = self.imageView.frame;
if (contentsFrame.size.width < boundsSize.width)
{
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
}
else
{
contentsFrame.origin.x = 0.0f;
}
if (contentsFrame.size.height < boundsSize.height)
{
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
}
else
{
contentsFrame.origin.y = 0.0f;
}
self.imageView.frame = contentsFrame;
}
//for zooming in and zooming out
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
if (self.zoomScale > 1.0f)
{
[self.observer photoZoomStarted];
[self centerScrollViewContents];
}
else
{
self.bouncesZoom = NO;
[self.observer photoZoomStopped];
// for zooming out by pinching
[self centerScrollViewContents];
}
// The scroll view has zoomed, so we need to re-center the contents
}
- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
Please let me know if it helps. Thanks :)

UIScrollView squashes UIImageView image

I have the following code in ViewDidLoad and it squashes my UIImage/ UIImageView. I want the image to fill the UIScrollView.
[super viewDidLoad];
_imageHolder = [[UIImageView alloc]initWithFrame:self.view.frame];
_scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
[_scrollView setBackgroundColor:[UIColor purpleColor]];
// Tell the scroll view the size of the contents
self.scrollView.contentSize = self.view.frame.size;
self.scrollView.delegate = self;
self.scrollView.scrollEnabled = YES;
FfThumbnailData* thumbData = [self.arrayOfImages objectAtIndex:self.thisImageIndex];
UIImage* fullSizedImage = [[UIImage alloc]initWithContentsOfFile:thumbData.path];
[self.imageHolder setImage:fullSizedImage];
float x = fullSizedImage.size.width;
float y = fullSizedImage.size.height;
CGRect rect = CGRectMake(0, 0, x, y);
NSLog(#"Size of image: %#", NSStringFromCGRect(rect));
//
//[self.imageHolder setImage:[UIImage imageNamed:#"P1020486.png"]];
[self.scrollView addSubview:_imageHolder];
[self.view addSubview:self.scrollView];
_imageHolder.contentMode = UIViewContentModeScaleAspectFit;
I set the backgound of the UIScrollView to purple for clarity:
http://i1219.photobucket.com/albums/dd427/Dave_Chambers/IMG_0315.png
I should add that after I zoom in and then zoom out, the image IS correctly placed in the scrollview and not squashed.
Strangely, the commented line [self.imageHolder setImage:[UIImage imageNamed:#"P1020486.png"]]; correctly displays the dummy image P1020486.png.
Also, when I download my app data, the image looks right and is indeed the correct size of an iPad image - {1936, 2592} - the size reported by my NSLog line NSLog(#"Size of image: %#", NSStringFromCGRect(rect));
Further, before I needed a UIScrollView (for zooming the photo in the full size View Controller) I access the same image and display it via an animation from one View Controller to another with the following code and it displays correctly. Crucially thought, this second section of code lacks a UIScollview.
-(void)animateTransition:(int)buttonInPositon {
#try {
FfThumbnailData* thumbData = [self.images objectAtIndex:buttonInPositon];
UIImage* fullSizedImage = [[UIImage alloc]initWithContentsOfFile:thumbData.path];
fullSize = [[FullSizeViewController alloc]init];
fullSize.imageHolderGhost.contentMode = UIViewContentModeScaleAspectFit;
fullSize.arrayOfImages = self.images;
fullSize.imageToPresent = fullSizedImage;
fullSize.numberOfImages = self.images.count;
fullSize.thisImageIndex = buttonInPositon;
[fullSize.imageHolderGhost setImage:fullSizedImage];
float screenWidth = self.view.bounds.size.width;
float screenHeight = self.view.bounds.size.height;
NSLog(#"ButtonFrame: %#", NSStringFromCGRect(buttonFrame));
[fullSize.view setFrame:CGRectMake(buttonFrame.origin.x, buttonFrame.origin.y, buttonFrame.size.width, buttonFrame.size.height-44)];
NSLog(#"ButtonFrame: %#", NSStringFromCGRect(fullSize.view.frame));
[self.view addSubview:fullSize.view];
fullSize.view.backgroundColor = [UIColor clearColor];
[UIView animateWithDuration:0.9 delay:0 options:UIViewAnimationCurveEaseIn animations:^{
[fullSize.view setFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
} completion:^(BOOL finished){
[[self navigationController] pushViewController:fullSize animated:NO];
self.navigationController.navigationBarHidden = YES;
}];
}
#catch (NSException *exception) {
NSLog(#"%#", exception);
}
}
For completeness, the code with the UIScrollView has the following method in one VC:
-(void)presentWithScrollview:(int)buttonInPositon {
FfThumbnailData* thumbData = [self.images objectAtIndex:buttonInPositon];
UIImage* fullSizedImage = [[UIImage alloc]initWithContentsOfFile:thumbData.path];
fullSize = [[FullSizeViewController alloc]init];
fullSize.arrayOfImages = self.images;
fullSize.imageToPresent = fullSizedImage;
fullSize.numberOfImages = self.images.count;
fullSize.thisImageIndex = buttonInPositon;
[[self navigationController] pushViewController:fullSize animated:NO];
self.navigationController.navigationBarHidden = YES;
}
and in the destination VC the following methods may be relevant to my problem (ViewDidLoad pasted at the top of this question):
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Set up the minimum & maximum zoom scales
CGRect scrollViewFrame = self.scrollView.frame;
CGFloat scaleWidth = scrollViewFrame.size.width / self.scrollView.contentSize.width;
CGFloat scaleHeight = scrollViewFrame.size.height / self.scrollView.contentSize.height;
CGFloat minScale = MIN(scaleWidth, scaleHeight);
//
self.scrollView.minimumZoomScale = minScale;
//Can set this bigger if needs be - match iPhoto
self.scrollView.maximumZoomScale = 2.0f;
[self centerScrollViewContents];
}
- (void)centerScrollViewContents {
CGSize boundsSize = self.scrollView.bounds.size;
//NSLog(#"boundsSize: %#", NSStringFromCGSize(boundsSize));
CGRect contentsFrame = self.imageHolder.frame;
//NSLog(#"contentsFrame: %#", NSStringFromCGRect(contentsFrame));
if (contentsFrame.size.width < boundsSize.width) {
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
} else {
contentsFrame.origin.x = 0.0f;
}
if (contentsFrame.size.height < boundsSize.height) {
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
} else {
contentsFrame.origin.y = 0.0f;
}
self.imageHolder.frame = contentsFrame;
}
#pragma mark - UIScrollViewDelegate
- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
// Return the view that we want to zoom
return self.imageHolder;
//return self.scrollViewBar;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
// The scroll view has zoomed, so we need to re-center the contents
[self centerScrollViewContents];
}
Any help on this would be great. Thanks
So, I found the solution myself. For some reason, as I wrote in my update to the question, any zooming in, followed by zooming out of the image fixed the image problem and presented it properly. I added these two lines to viewWillAppear, above [self centerScrollViewContents];
self.scrollView.zoomScale = 2.0;
self.scrollView.zoomScale = 1.0;
Problem solved

Resources