UIScrollView 缩放图片

UIView* wholeview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024,768)];
    [wholeview setBackgroundColor:[UIColor clearColor]];
    
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"bk" ofType:@"png"];
    UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
    
    UIScrollView* mybkScrollView = [[UIScrollView alloc] initWithFrame:wholeview.bounds];
    mybkScrollView.minimumZoomScale = 1.0;   //最小倍数
    mybkScrollView.maximumZoomScale = 2.0;  //最大倍数
    mybkScrollView.contentSize = CGSizeMake(image.size.width,image.size.height); //两倍大小图片
    mybkScrollView.delegate = self;
    mybkScrollView.decelerationRate = 1.0f; //减速速率
    
    mybkScrollView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    
    //放大缩小的图片
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:mybkScrollView.bounds];
    imageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    imageView.image = image;
    [mybkScrollView addSubview:imageView];
    [imageView release];
    
    [wholeview addSubview:mybkScrollView];
    [mybkScrollView release];
   
    [self.view addSubview:wholeview];
    [wholeview release];
    
    mybkScrollView.zoomScale = 1.0;  初始时的大小。不然初始拖动会显示底色的

你可能感兴趣的:(image,UIView)