2012-07-04 81 views
2

我必須在UIScrollView中縮放圖像,並且在縮放後,我想裁剪在UIImageView中可見的圖像。我有以下問題:在scrollview iphone上縮放圖像?

1)圖像不縮放,viewForZoomingInScrollView方法未被調用。

2)這是我關於裁剪的第二個問題,第一個問題是here。如何在縮放後裁剪縮放的圖像?

我的代碼是:

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

@end 

和.m文件

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

} 

回答

5

實施UIScrollViewDelegate並在控制器的viewDidLoad中調用這個函數。

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

,寫此功能

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

感謝,但是這個代碼也沒有爲我工作。 :( – QueueOverFlow

+0

在你發佈imageView1的一種方式?爲什麼? –

+0

一個建議在XIB中採取所有這個UIScrollView和UIImageView,那麼它將很容易爲你.. –