【问题标题】:zoom image on scrollview iphone?在滚动视图 iPhone 上缩放图像?
【发布时间】:2012-07-04 19:36:43
【问题描述】:

我必须在UIScrollView 中缩放图像,缩放后我想裁剪UIImageView 中可见的图像。我有以下问题:

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;

}

【问题讨论】:

    标签: iphone objective-c ios scrollview


    【解决方案1】:

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

    【讨论】:

    • 以一种方式发布 imageView1?为什么?
    • 一个建议把所有这些 UIScrollView 和 UIImageView 放在 XIB 中,这样对你来说会很容易..
    • 仍然无法正常工作.. 委托方法 viewForZoomingInScrollView 未再次调用
    • 谢谢.. 它现在可以工作了.. 问题是我的图像视图和滚动视图更小,我正在模拟器上进行测试.. 非常感谢您帮助我...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    相关资源
    最近更新 更多