views:

173

answers:

1

i wanted to find an "easy" way to use the pinch/zoom function on my app! so i decided to use a UIScrollView. so far so good.

i load my image from an sqlite db like so:

        - (void)viewWillAppear:(BOOL)animated {
            imageView.image = entity.Aattribute;
        }


    - (void)viewDidLoad {
                self.title = @"Title";

        imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
        imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.backgroundColor = [UIColor blackColor];


            myScrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
            myScrollView.maximumZoomScale = 4.0;
            myScrollView.minimumZoomScale = 0.75;
            myScrollView.clipsToBounds = YES;
            myScrollView.delegate = self;

            [myScrollView addSubview:imageView];


 self.view = myScrollView;



        }


        - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
            return imageView;
        }

any help would be appreciated!

thank you for your time!

EDIT: i m just gonna answer my own question here! (i m getting better at this!!! LOL)

up above is the working code. i have edited it so if anyone needs this can refer to it! thanks for the answers!

A: 

This looks mostly correct. A couple of notes:

  • you've misspelled "viewDidLoad". This could be a side effect of typing it into the browser window. If it's wrong in your code, then it's certainly not helping things be better.
  • viewForZoomingInScrollView: is expecting you to return a UIView. myImage sounds suspiciously unlike a UIView. You should be returning the imageView instance variable. That's what's getting moved around.

Edit

From the documentation:

The UIScrollView class can have a delegate that must adopt the UIScrollViewDelegate protocol. For zooming and panning to work, the delegate must implement both viewForZoomingInScrollView: and scrollViewDidEndZooming:withView:atScale:; in addition, the maximum (maximumZoomScale) and minimum ( minimumZoomScale) zoom scale must be different.

So it looks like you still need to implement another delegate method.

Dave DeLong
hello there! thank you for your response. the code i wrote was out of my head but i have corrected it by copy/paste from my project.what happens is that the image is loading BUT no zoom AT ALL!
treasure
hi there. i do adopt the UIScrollView delegate method. however in ALL the tutorials i ve seen noone implements the scrollViewDidEndZooming:withView:atScale: etc etc. the sample code works just fine. my code works just fine as long as i do not import my sqlite data image! (i use ImageToDataTransformer) in my xcdatamodel.this is really weird!
treasure
@treasure if your code works find, unless you're importing the image from sqlite, then why on earth didn't you post the code that's not working?!
Dave DeLong
actually this is a not working code!i said when i use this in the viewdidload method: UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.austinbull.com/clonemines.png"]]]]; [self setMyImage:tempImageView]; [tempImageView release];it works ok!BUT when i use this: - (void)viewWillAppear:(BOOL)animated { imageView.image = entity.Aattribute; } to load my image it doesnt!
treasure
i have answered my own question here! thanks for your time!
treasure