照片库uiscrollView滚动和缩放

时间:2011-09-15 09:21:14

标签: iphone xcode4 uiscrollview photo-gallery

我想用两个滚动视图创建照相馆 一个用于图像之间的水平滚动,另一个用于缩放图像。

在图像之间滚动工作正常,但我无法将图像缩放到第二个scrollView。

mainScrollView名为VcLicences

VcLicences.h

#import <UIKit/UIKit.h>


@interface VcLicences : UIViewController <UIScrollViewDelegate>{
    IBOutlet UIScrollView* scrollView;
IBOutlet UIPageControl* pageControl;

    BOOL pageControlIsChangingPage;
}

@property (nonatomic, retain) UIView *scrollView;
@property (nonatomic, retain) UIPageControl* pageControl;

/* for pageControl */
- (IBAction)changePage:(id)sender;

/* internal */
- (void)setupPage;

@end

VcLicences.m

#import "VcLicences.h"
#import "VcImageZoomView.h"


@implementation VcLicences

@synthesize scrollView;
@synthesize pageControl;



- (void)setupPage
{
    scrollView.delegate = self;

    [self.scrollView setBackgroundColor:[UIColor whiteColor]];

    scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
    scrollView.clipsToBounds = YES;
    scrollView.scrollEnabled = YES;
    scrollView.pagingEnabled = YES;
    scrollView.multipleTouchEnabled = NO;


    NSUInteger nim = 0;
    CGFloat cx = 0;

    NSArray *licence = [NSArray arrayWithObjects:@"zl1.jpg", @"zl2.jpg", @"zl3.jpg", @"dph.jpg", @"or.jpg", nil];

    for (nim=0; nim < [licence count]; nim++) 
    {
        VcImageZoomView *imageZoom = [[VcImageZoomView alloc] initWithNibName:@"VcImageZoomView" bundle:nil];

        [imageZoom.view setFrame:CGRectMake(cx, 0, 320, 332)];

        [imageZoom setMyImage:[licence objectAtIndex:nim]];

        [scrollView addSubview:imageZoom.view];

        [imageZoom release];

        cx += scrollView.frame.size.width;
    }

    self.pageControl.numberOfPages = nim;

    [scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
}

#pragma mark UIScrollViewDelegate stuff
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
    if (pageControlIsChangingPage) 
    {
        return;
    }

    CGFloat pageWidth = _scrollView.frame.size.width;
    int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    pageControl.currentPage = page;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView 
{
    pageControlIsChangingPage = NO;
}

- (IBAction)changePage:(id)sender 
{
    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * pageControl.currentPage;
    frame.origin.y = 0;

    [scrollView scrollRectToVisible:frame animated:YES];

    pageControlIsChangingPage = YES;
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    self.navigationItem.title = @"CZECHMAT's licenses";
    [self setupPage];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload 
{
    //[scrollView release];
    //[pageControl release];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    //return (interfaceOrientation == UIInterfaceOrientationPortrait);
    return YES;
}

@end

第二个ScrollView用于缩放名为VcImageZoomView的图像

VcImageZoomView.h

#import <UIKit/UIKit.h>

@interface VcImageZoomView : UIViewController  <UIScrollViewDelegate>{
    IBOutlet UIScrollView *scroll;
    UIImageView *image;
}

@property (nonatomic, retain) UIScrollView *scroll;

-(void)setMyImage:(NSString *) imageName;


@end

VcImageZoomView.m

#import "VcImageZoomView.h"

@implementation VcImageZoomView

@synthesize  scroll;

-(void)setMyImage:(NSString *) imageName;
{
    NSLog(@"%@", imageName);
    image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];

    float scaleWidth = scroll.frame.size.width / image.frame.size.width;
    float scaleHeight = scroll.frame.size.height / image.frame.size.height;

    float scale = scaleWidth < scaleHeight ? scaleWidth : scaleHeight;


    NSLog(@"%f", scale);

    [scroll addSubview:image];
    scroll.minimumZoomScale = scale;
    scroll.maximumZoomScale = 1.0;
    scroll.delegate = self;

    [scroll setZoomScale:scroll.minimumZoomScale];

}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{    
    [super viewDidLoad];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

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

@end

thx for way

2 个答案:

答案 0 :(得分:2)

你真的需要看看Apple的PhotoViewer示例以及关于ScrollViews的WWDC'10和'11视频:他们正在详细解释(通过这个照片浏览器滚动和缩放的例子)如何实现它:

  • 如何回收内存,以便大幅减少内存占用(对流动性非常重要)

  • 如何处理在一个视图中滚动和放大另一个视图

  • 如果需要,如何进行图像平铺以在缩放时优化内存(仅在预先计算平铺时有用,所以如果你的包中还有图像)

这是非常有趣的教程和视频,非常值得阅读/观看。

答案 1 :(得分:0)

https://github.com/doubleencore/KTPhotoBrowser

找到上面附带的链接获取照片库的源代码.....