xcode 如何缩放图像xcode?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11611618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to do zooming image xcode??
提问by user1498513
i have a problem. I have this code a zoom doesn't work.
我有个问题。我有这个代码缩放不起作用。
- (void)viewDidLoad {
- (void)viewDidLoad {
UIImage *image = [UIImage imageNamed:@"zoomimage.jpeg"];
CGRect yFrame = CGRectMake(0.0, 0.0, 320, 432);
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height)];
[imageView setImage:image];
scrollView = [[UIScrollView alloc] initWithFrame:yFrame];
[scrollView setScrollEnabled:YES];
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setContentSize:CGSizeMake(image.size.width, image.size.height)];
scrollView.minimumZoomScale = 0.4;
scrollView.maximumZoomScale = 4.0;
[scrollView setZoomScale:scrollView.minimumZoomScale];
[self.view addSubview:scrollView];
[self.view bringSubviewToFront:scrollView];
[scrollView addSubview:imageView];
}
}
回答by Slim Shaddy
First, set the delegate of scrollview to your self.
首先,将 scrollview 的委托设置为您自己。
scrollView.delegate = self;
Now, Add this delegate method of UIScrollView
in your class to enable zooming on the image.
现在,UIScrollView
在您的类中添加此委托方法以启用图像缩放。
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imageView;
}
回答by tnylee
Try this:
尝试这个:
.h Controller:
.h 控制器:
@interface MyViewController : UIViewController{
IBOutlet UIScrollView *scrollView;
UIImageView *imageView;
}
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIImageView *imageView;
.m Controller:
.m 控制器:
@synthesize scrollView, imageView;
-(UIView *)viewForZoomingInScrollView:(UIScrollVi-ew *)scrollView{
return imageView;
}
- (void)viewDidLoad
{
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"YourImageName.png"]];
self.imageView = tempImageView;
[tempImageView release];
scrollView.maximumZoomScale = 3.0;
scrollView.minimumZoomScale = 0.6;
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
[scrollView addSubview:imageView];
}
This code worked for me :)
这段代码对我有用:)