簡単に画像を拡大して表示する方法
iPhoneで画像を拡大して表示する方法ですが、Google先生に聞いたところ How to scale a UIImageView proportionally ? - Stack Overflow というのが見つかりました。しかし、UIScrollViewを使うともっと簡単に拡大表示できました。
UIScrollViewは大きな画面等をスクロールして表示するためのViewですが、拡大縮小の機能も持っています。 これを使って画像を拡大して表示する事ができます。
.hファイル
#import <UIKit/UIKit.h> @interface Ex1ViewController : UIViewController <UIScrollViewDelegate> { IBOutlet UIImageView *imageView; // 原寸画像表示 IBOutlet UIImageView *imageView2; // 作業用(上のコピー) IBOutlet UIScrollView *scrollView; // 拡大画像表示 } @end
.mファイル(部分)
#import "Ex1ViewController.h" @implementation Ex1ViewController - (void)viewDidLoad { [super viewDidLoad]; imageView2 = [[UIImageView alloc] initWithImage:imageView.image]; scrollView.maximumZoomScale = 10; scrollView.zoomScale = 2.5; scrollView.delegate = self; scrollView.userInteractionEnabled = NO; [scrollView addSubview:imageView2]; } - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return imageView2; }
userInteractionEnabled = NO を指定していますが、これが無いとピンチ動作で画像の大きさが変更出来てしまいます。
また、scrollViewに表示するUIImageViewに imageView を使うと原寸の画像が表示されなくなってしまったので、 imageViewのコピー imageView2を作っています。原寸の表示が不要なら imagwView をそのまま使ってかまいません。