iOS scroll view subviews - ios

I have 3 scroll views which I added in IB then in my viewDidLoad I call a method that add a UIImageView to each subview. I would like to have zooming on each subview so I use
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [scrollView.subviews objectAtIndex:0];
}
but viewForZoomingInScrollView is getting called before the imageviews are added to the subviews. How can solve this?
This is the loadImages method:
-(void) loadImages{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *imageData1 = [defaults dataForKey:#"image1"];
NSData *imageData2 = [defaults dataForKey:#"image2"];
NSData *imageData3 = [defaults dataForKey:#"image3"];
UIImage *img1 = [UIImage imageWithData:imageData1];
UIImage *img2 = [UIImage imageWithData:imageData2];
UIImage *img3 = [UIImage imageWithData:imageData3];
[self loadImage:img1 toScrollView:scrollViewTop];
[self loadImage:img2 toScrollView:scrollViewLeft];
[self loadImage:img3 toScrollView:scrollViewRight];
}
-(void) loadImage:(UIImage *)image toScrollView:(UIScrollView *)scrollView
{
//scale and rotate image to default
UIImage *scaledImage = scaleAndRotateImage(image);
//create imageview and add image to the view
UIImageView *imageView = [[UIImageView alloc]initWithImage:scaledImage];
CGFloat imageWidth = scaledImage.size.width;
CGFloat imageHeight = scaledImage.size.height;
int scrollWidth = scrollView.frame.size.width;
int scrollHeight = scrollView.frame.size.height;
//Limit by width or height, depending on which is smaller in relation to
//the scrollview dimension.
float scaleX = scrollWidth / imageWidth;
float scaleY = scrollHeight / imageHeight;
float scaleScroll = (scaleX < scaleY ? scaleY : scaleX);
// scrollView.bounds = CGRectMake(0, 0,imageWidth , imageHeight );
//scrollView.frame = CGRectMake(0, 0, scrollWidth, scrollHeight);
scrollView.delegate = self;
scrollView.contentSize = imageView.frame.size;
scrollView.pagingEnabled = NO;
scrollView.maximumZoomScale = scaleScroll*3;
scrollView.minimumZoomScale = scaleScroll;
scrollView.zoomScale = scaleScroll;
[scrollView addSubview:imageView];
}

Try adding the image view as a subview before you configure zooming. Say, before the delegate assignment.
viewForZoomingInScrollView: is getting called by one of the scroll view's setters; looking at the stack trace from there should tell you which one.

Related

iOS: Make image width slightly smaller than screen width

I'm having an issue where my image is too big. I need it to be slightly less than what the screen width is.
Here's my controller:
- (void)viewDidLoad {
[super viewDidLoad];
self.imageName = #"goldencoaster";
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"table"]]];
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"%#", self.imageName]];
CGRect bounds;
bounds.origin = CGPointZero;
bounds.size = [[UIScreen mainScreen] bounds].size;
self.coasterImage.bounds = bounds;
self.coasterImage.image = image;
}
However, this returns this when I run the simulator:
How would I get the image to show up slightly smaller than the screen width?
====================================================================
UPDATE
So I took out auto layout as suggested, and updated my controller to look like this:
- (void)viewDidLoad {
[super viewDidLoad];
self.imageName = #"goldencoaster";
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"table"]]];
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"%#", self.imageName]];
CGRect screen = [[UIScreen mainScreen] bounds];
CGRect bounds;
bounds.origin.x = screen.origin.x + 10;
bounds.origin.y = screen.origin.y + 10;
bounds.size.width = screen.size.width - 100;
self.coasterImage.frame = bounds;
self.coasterImage.image = image;
}
However, now my image doesn't show up at all?? Only the background (image with name "table") shows up, but not the goldencoaster.
Try this
- (void)viewDidLoad {
[super viewDidLoad];
self.imageName = #"goldencoaster";
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"table"]]];
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"%#", self.imageName]];
CGRect bounds;
bounds.origin = CGPointZero;
bounds.size = [[UIScreen mainScreen] bounds].size;
self.coasterImage.bounds = bounds;
self.coasterImage.image = image;
self.coasterImage.frame = CGRectMake(20, 0, self.view.frame.size.width-40, self.view.frame.size.height);
self.coasterImage.center = self.coasterImage.superview.center;
}
Swift 3.1
override func viewDidLoad() {
super.viewDidLoad()
imageName = "goldencoaster"
view.backgroundColor = UIColor(patternImage: UIImage(named: "table")!)
let image = UIImage(named: imageName)
var bounds = CGRect()
bounds.origin = CGPoint.zero
bounds.size = (UIScreen.main.bounds).size
coasterImage.bounds = bounds
coasterImage.image = image
coasterImage.frame = CGRect(x: CGFloat(20), y: 0, width: self.view.frame.size.width - CGFloat(40), height: self.view.frame.size.height)
coasterImage.center = (coasterImage.superview?.center)!
}
you can do it with contentMode like
self.coasterImage.contentMode = UIViewContentModeScaleAspectFit;
here are the different contentMode check it from here
UIViewContentModeScaleToFill
UIViewContentModeScaleAspectFit // contents scaled to fit with fixed aspect. remainder is transparent
UIViewContentModeScaleAspectFill // contents scaled to fill with fixed aspect. some portion of content may be clipped.
UIViewContentModeRedraw // redraw on bounds change (calls -setNeedsDisplay)
UIViewContentModeCenter // contents remain same size. positioned adjusted.
UIViewContentModeTop
UIViewContentModeBottom
UIViewContentModeLeft
UIViewContentModeRight
UIViewContentModeTopLeft
UIViewContentModeTopRight
UIViewContentModeBottomLeft
UIViewContentModeBottomRight
Two words: Auto Layout
You want your UI to adapt to different devices, and orientations.
Auto Layout and size classes are designed to handle those needs.
Update:
You're checking screen size and setting bounds. That suggests you're trying to size the view instead of letting Auto Layout constrain it. Auto Layout takes the place of setting frame or bounds. You don't want to mix the two.
If you weren't using autolayout, you will need to edit the image frame. For example if you want to reduce the size in 10 pixels:
- (void)viewDidLoad {
[super viewDidLoad];
self.imageName = #"goldencoaster";
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"table"]]];
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"%#", self.imageName]];
CGRect screen = [[UIScreen mainScreen] bounds];
CGRect bounds = CGRectMake(screen.origin.x+10,
screen.origin.y+10,
screen.size.width-20,
screen.size.height-20);
self.coasterImage.center = self.coasterImage.superview.center;
self.coasterImage.frame = bounds;
self.coasterImage.image = image;
}

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

how to change CropRect when it is loaded?

I found a useful library which works with cropping image in this link. I want to use it with only one cropRect which is ratio = 3.0f / 4.0f; instead of choosing them. I added below code in viewDidApper method of PECropViewController:
CGFloat ratio = 3.0f / 4.0f;
CGRect cropRect = self.cropView.cropRect;
CGFloat width = CGRectGetWidth(cropRect);
cropRect.size = CGSizeMake(width, width * ratio);
self.cropView.cropRect = cropRect;
But it is not working. There is (void)constrain:(id)sender method which works when I press Edit button. If I write above code in this method it works well. Here is the code:
(void)constrain:(id)sender
{
CGFloat ratio = 3.0f / 4.0f;
CGRect cropRect = self.cropView.cropRect;
CGFloat width = CGRectGetWidth(cropRect);
cropRect.size = CGSizeMake(width, width * ratio);
self.cropView.cropRect = cropRect;
return;
{
The problem is I can't change cropRect's ratio when croppingView appears.
if somebody is looking for solution for this question, i found it. I have just changed cropAspectRatio in PECropView:
- (void)setupImageView
{
CGRect cropRect = AVMakeRectWithAspectRatioInsideRect(self.image.size, self.insetRect);
self.scrollView.frame = cropRect;
self.scrollView.contentSize = cropRect.size;
self.zoomingView = [[UIView alloc] initWithFrame:self.scrollView.bounds];
self.zoomingView.backgroundColor = [UIColor clearColor];
[self.scrollView addSubview:self.zoomingView];
self.imageView = [[UIImageView alloc] initWithFrame:self.zoomingView.bounds];
self.imageView.backgroundColor = [UIColor clearColor];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.image = self.image;
[self.zoomingView addSubview:self.imageView];
//i added this line code
self.cropAspectRatio = 4 / 3.0;
}

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