简体   繁体   中英

zoom image on scrollview iphone?

I have to zoom an image in UIScrollView and after zooming i wants crop the image which is visible in UIImageView . I have the following problems:

1) Image not zooming, viewForZoomingInScrollView method not called.

2) This is my second question about cropping, the first is here . How can I crop the zoomed image after zooming?

My code is:

@interface ViewController : UIViewController<UIScrollViewDelegate>
{
     UIImageView *imageView1;
     UIScrollView *scroll;
}
@property(nonatomic,retain) UIImageView *imageView1;

@end

and in .m file

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    CGRect frame1 = CGRectMake(10,10,190,200);
    self.imageView1.userInteractionEnabled = YES;

    imageView1=[[UIImageView alloc]initWithFrame:frame1];
    imageView1.contentMode = UIViewContentModeScaleToFill;



    imageView1.image= [UIImage imageNamed:@"back.jpeg"];



    CGRect frame = CGRectMake(10,10,190,200);
    scroll = [[UIScrollView alloc] initWithFrame:frame];
    scroll.contentSize = CGSizeMake(240, 260);
    scroll.showsHorizontalScrollIndicator = YES;
    scroll.showsVerticalScrollIndicator = YES;  
    scroll.clipsToBounds = YES;

    scroll.backgroundColor=[UIColor blackColor];
    [scroll addSubview:imageView1];
    [imageView1 release];

    scroll.minimumZoomScale = 0.55; 
    scroll.maximumZoomScale = 2.0;

    scroll.delegate=self;

    [self.view addSubview:scroll];

    self.imageView1.userInteractionEnabled = YES;

}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    NSLog(@"delegate method called");
    return self.imageView1;

}

Implement UIScrollViewDelegate and call this function in ViewDidLoad of your controller.

-(void)setContentSizeForScrollView
{
    scrollView.contentSize = CGSizeMake(YOURIMAGEVIEW.frame.size.width, YOURIMAGEVIEW.frame.size.height);
    scrollView.minimumZoomScale = 1.0;
    scrollView.maximumZoomScale = 5.0;
}

and write this function

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM