UIScrollView 中的 iPhone UIImageView 未缩放到中心

发布于 2024-11-16 20:32:49 字数 985 浏览 3 评论 0原文

我在 UIScrollView 中有一个 UIImageView,所以我可以允许缩放,缩放可以工作,但它不会保持 UIImageView 居中并最终剪切离开图像的底部并在顶部留下黑色空间。

这就是我的情况:有

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    scroller.bouncesZoom   = YES;
    scroller.clipsToBounds = YES;

    float scale = image.size.width/320;
    UIImage *scaled = [UIImage imageWithCGImage:[image CGImage] scale:scale orientation:UIImageOrientationUp];

    imageView = [[UIImageView alloc] initWithImage:scaled];
    imageView.userInteractionEnabled = YES;
    [imageView setCenter:CGPointMake(160,240)];

    [scroller addSubview:imageView];
    scroller.contentSize = [imageView frame].size;

    scroller.maximumZoomScale = 4.0;
    scroller.minimumZoomScale = 1.0;
    scroller.zoomScale        = 1.0;
}

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

什么建议吗?

I have a UIImageView inside a UIScrollView so I can allow zooming, the zooming works but it doesn't keep the UIImageView centered and ends up cutting off the bottom of the image and leaves black space on top.

This is what I have:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    scroller.bouncesZoom   = YES;
    scroller.clipsToBounds = YES;

    float scale = image.size.width/320;
    UIImage *scaled = [UIImage imageWithCGImage:[image CGImage] scale:scale orientation:UIImageOrientationUp];

    imageView = [[UIImageView alloc] initWithImage:scaled];
    imageView.userInteractionEnabled = YES;
    [imageView setCenter:CGPointMake(160,240)];

    [scroller addSubview:imageView];
    scroller.contentSize = [imageView frame].size;

    scroller.maximumZoomScale = 4.0;
    scroller.minimumZoomScale = 1.0;
    scroller.zoomScale        = 1.0;
}

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

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诗化ㄋ丶相逢 2024-11-23 20:32:50

如果不需要分页,则禁用分页,它可以正常工作

scrollview.pagingEnabled = NO;

If pagination is not needed then disable pagination it works fine

scrollview.pagingEnabled = NO;

故事↓在人 2024-11-23 20:32:50

下面是我在应用程序中使用的代码(改编自 WWDC 示例)也许您可以从中获取一些东西:)

    CGSize imageSize =[image size];
    CGSize boundsSize = scrollview.bounds.size;
    CGRect frameToCenter = imageview.frame;

    // center horizontally
    if (frameToCenter.size.width < boundsSize.width)
        frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
    else
        frameToCenter.origin.x = 0;

    // center vertically
    if (frameToCenter.size.height < boundsSize.height)
        frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
    else
        frameToCenter.origin.y = 0;

    imageview.frame = frameToCenter;

    CGFloat xScale = boundsSize.width / imageSize.width;    // the scale needed to perfectly fit the image width-wise
    CGFloat yScale = boundsSize.height / imageSize.height;  // the scale needed to perfectly fit the image height-wise
    CGFloat minScale = MIN(xScale, yScale);                 // use minimum of these to allow the image to become fully visible

    // on high resolution screens we have double the pixel density, so we will be seeing every pixel if we limit the
    // maximum zoom scale to 0.5.
    CGFloat maxScale = 1.0 / [[UIScreen mainScreen] scale];

    // don't let minScale exceed maxScale. (If the image is smaller than the screen, we don't want to force it to be zoomed.) 
    if (minScale > maxScale) {
        minScale = maxScale;
    }

    scrollview.contentSize=imageSize;
    scrollview.maximumZoomScale=maxScale;
    scrollview.minimumZoomScale=minScale;
    scrollview.zoomScale=1;

Below is the code I've used in my app (adapted from the WWDC sample) Maybe you can take something out of it :)

    CGSize imageSize =[image size];
    CGSize boundsSize = scrollview.bounds.size;
    CGRect frameToCenter = imageview.frame;

    // center horizontally
    if (frameToCenter.size.width < boundsSize.width)
        frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
    else
        frameToCenter.origin.x = 0;

    // center vertically
    if (frameToCenter.size.height < boundsSize.height)
        frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
    else
        frameToCenter.origin.y = 0;

    imageview.frame = frameToCenter;

    CGFloat xScale = boundsSize.width / imageSize.width;    // the scale needed to perfectly fit the image width-wise
    CGFloat yScale = boundsSize.height / imageSize.height;  // the scale needed to perfectly fit the image height-wise
    CGFloat minScale = MIN(xScale, yScale);                 // use minimum of these to allow the image to become fully visible

    // on high resolution screens we have double the pixel density, so we will be seeing every pixel if we limit the
    // maximum zoom scale to 0.5.
    CGFloat maxScale = 1.0 / [[UIScreen mainScreen] scale];

    // don't let minScale exceed maxScale. (If the image is smaller than the screen, we don't want to force it to be zoomed.) 
    if (minScale > maxScale) {
        minScale = maxScale;
    }

    scrollview.contentSize=imageSize;
    scrollview.maximumZoomScale=maxScale;
    scrollview.minimumZoomScale=minScale;
    scrollview.zoomScale=1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文