图像未在滚动视图中垂直居中

问题描述 投票:1回答:2

我想念的是什么?内部滚动视图和图像视图填充整个屏幕。但是不知何故我的形象没有居中。图像的左上角从视图的中心开始,但是我想使图像很好地居中。也在缩放过程中。

-(void)prepareScrollView
{
    for(int i =0;i<[self.layoverPhotoAssets count];i++){
        PHAsset *asset = self.layoverPhotoAssets[i];

        FMImageZoomViewController *zoomController = [[FMImageZoomViewController alloc] init];
//        UIImageView *imageView = [[UIImageView alloc] init];
        int x = self.scrollView.frame.size.width * i;
        zoomController.view.frame = CGRectMake(x, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
        //zoomController.view.frame = CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height);
        [self.scrollView addSubview:zoomController.view];
        zoomController.zoomScroller.delegate = self;
        zoomController.imageView.tag = 1;
        [self.zoomControllers addObject:zoomController];

        PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
        options.resizeMode = PHImageRequestOptionsResizeModeFast;
        options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat; //I only want the highest possible quality
        options.synchronous = NO;
        options.networkAccessAllowed = YES;
        [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:zoomController.zoomScroller.frame.size contentMode:PHImageContentModeAspectFill options:options resultHandler:^(UIImage *result, NSDictionary *info) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if(result){
                zoomController.imageView.image = result;
                zoomController.imageView.backgroundColor = [UIColor redColor];
            }
        });
        }];

        //self.scrollView.contentSize= ;
    }
    [self.scrollView setContentSize:CGSizeMake(self.scrollView.frame.size.width * [self.layoverPhotoAssets count], 0)];
    [self scrollToAsset:self.selectedAsset];
}
objective-c uiscrollview uiimageview
2个回答
0
投票

考虑:

    zoomController.view.frame = CGRectMake(x, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
    [self.scrollView addSubview:zoomController.view];

那不可能是对的。如果zoomController.view将作为self.scrollView的子视图,则其在self.scrollView中的帧是根据self.scrollViewbounds,而不是self.scrollViewframe


0
投票

解决了这个问题:

-(void)prepareScrollView
{
    for(int i =0;i<[self.layoverPhotoAssets count];i++){
        PHAsset *asset = self.layoverPhotoAssets[i];

        FMImageZoomViewController *zoomController = [[FMImageZoomViewController alloc] init];
//        UIImageView *imageView = [[UIImageView alloc] init];
        int x = self.scrollView.frame.size.width * i;
        zoomController.view.frame = CGRectMake(x, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height);
        //zoomController.view.frame = CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height);
        [self.scrollView addSubview:zoomController.view];
        zoomController.zoomScroller.delegate = self;
        [self.zoomControllers addObject:zoomController];

        PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
        options.resizeMode = PHImageRequestOptionsResizeModeExact;
        options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat; //I only want the highest possible quality
        options.synchronous = NO;
        options.networkAccessAllowed = YES;
        [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:zoomController.zoomScroller.bounds.size   contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage *result, NSDictionary *info) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if(result){
                zoomController.imageView = [[UIImageView alloc] initWithImage:result];
                zoomController.imageView.frame = zoomController.zoomScroller.bounds;
                [zoomController.imageView setContentMode:UIViewContentModeScaleAspectFit];
                zoomController.imageView.clipsToBounds = YES;
                [zoomController.imageView setCenter: self.scrollView.center];
                zoomController.imageView.tag = 1;
                [zoomController.zoomScroller addSubview:zoomController.imageView];

//                zoomController.imageView.contentMode = UIViewContentModeCenter;
//                if (zoomController.imageView.bounds.size.width > result.size.width && zoomController.imageView.bounds.size.height > result.size.height) {
//                       zoomController.imageView.contentMode = UIViewContentModeScaleAspectFit;
//                }
            }
        });
        }];

        //self.scrollView.contentSize= ;
    }
    [self.scrollView setContentSize:CGSizeMake(self.scrollView.frame.size.width * [self.layoverPhotoAssets count], 0)];
    [self scrollToAsset:self.selectedAsset];
}
© www.soinside.com 2019 - 2024. All rights reserved.