缩放后无法在UIScrollView中设置图像

时间:2012-06-26 11:47:25

标签: iphone image uiscrollview uiimageview uiscrollviewdelegate

我使用ImageScrollView来显示图片。在开始我只显示小图像。当用户尝试缩放时,我会加载大图像。首先,我想使用此代码执行此操作:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollViewCalled
{
    ImageScrollView *scroll = (ImageScrollView *)scrollViewCalled;
    UIImageView *imageView = (UIImageView *)[scroll imageView];
    return imageView;
}

- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollViewCalled withView:(UIView *)view
{
    ImageScrollView *scroll = (ImageScrollView *)scrollViewCalled;
    if (scroll != scrollViewCalled || scroll.isImageBig)
    {
        return;
    }
    scroll.userInteractionEnabled = NO;
    [self setBigImageInBackgroundForIndex:nil];
}

但它很奇怪:当我尝试缩放时,会显示大图像,但我看到它的最大缩放比例,我无法滚动查看图像的其余部分。但是当我再缩放一次而不是滚动图像时,它开始正常工作,我终于可以滚动图像了。但我应该放大2倍。

然后我尝试scrollViewDidEndZooming代替scrollViewWillBeginZooming

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollViewCalled withView:(UIView *)view atScale:(float)scale
{
    ImageScrollView *scroll = (ImageScrollView *)scrollViewCalled;
    if (scroll != scrollViewCalled || scroll.isImageBig)
    {
        return;
    }
    scroll.userInteractionEnabled = NO;
    [self setBigImageInBackgroundForIndex:nil];
}

除了一件事,一切都变得完美:现在为时已晚。我缩放小图像,我看到它是缩放变体,然后加载大图像,我看到大图像的最小缩放。对于用户来说,这个图像回归到它的普遍变焦。只有当我再次尝试缩放时,我才能完美地缩放。

提前谢谢!

这是ImageScrollView的代码:

ImageScrollView.h
#import <UIKit/UIKit.h>

@interface ImageScrollView : UIScrollView <UIScrollViewDelegate> {
    UIView        *imageView;
    NSUInteger     index;
}
@property (assign) NSUInteger index;
@property (retain, nonatomic) UIView        *imageView;
@property (assign, nonatomic) BOOL isImageBig;
@property (assign, nonatomic) BOOL hasImage;

- (void)displayImage:(UIImage *)image;
- (void)displayTiledImageNamed:(NSString *)imageName size:(CGSize)imageSize;
- (void)setMaxMinZoomScalesForCurrentBounds;

- (CGPoint)pointToCenterAfterRotation;
- (CGFloat)scaleToRestoreAfterRotation;
- (void)restoreCenterPoint:(CGPoint)oldCenter scale:(CGFloat)oldScale;

@end

ImageScrollView.m

#import "ImageScrollView.h"
#import "TilingView.h"

@implementation ImageScrollView
@synthesize index;
@synthesize imageView;
@synthesize isImageBig, hasImage;

- (id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame])) {
        self.showsVerticalScrollIndicator = NO;
        self.showsHorizontalScrollIndicator = NO;
        self.bouncesZoom = YES;
        self.decelerationRate = UIScrollViewDecelerationRateFast;
        self.delegate = self;        
    }
    return self;
}

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

#pragma mark -
#pragma mark Override layoutSubviews to center content

- (void)layoutSubviews 
{
    [super layoutSubviews];

    // center the image as it becomes smaller than the size of the screen

    CGSize boundsSize = self.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;

    if ([imageView isKindOfClass:[TilingView class]]) {
        // to handle the interaction between CATiledLayer and high resolution screens, we need to manually set the
        // tiling view's contentScaleFactor to 1.0. (If we omitted this, it would be 2.0 on high resolution screens,
        // which would cause the CATiledLayer to ask us for tiles of the wrong scales.)
        imageView.contentScaleFactor = 1.0;
    }
}

#pragma mark -
#pragma mark UIScrollView delegate methods

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

#pragma mark -
#pragma mark Configure scrollView to display new image (tiled or not)

- (void)displayImage:(UIImage *)image
{
    // clear the previous imageView
    [imageView removeFromSuperview];
    [imageView release];
    imageView = nil;

    // reset our zoomScale to 1.0 before doing any further calculations
    self.zoomScale = 1.0;

    self.imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
    [self addSubview:imageView];

    self.contentSize = [image size];
    [self setMaxMinZoomScalesForCurrentBounds];
    self.zoomScale = self.minimumZoomScale;
//    self.zoomScale = self.maximumZoomScale;
}


- (void)setMaxMinZoomScalesForCurrentBounds
{
    CGSize boundsSize = self.bounds.size;
    CGSize imageSize = imageView.bounds.size;

    // calculate min/max zoomscale
    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];
//    CGFloat maxScale = 4.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;
    }

    self.maximumZoomScale = maxScale;
    self.minimumZoomScale = minScale;
}

@end

2 个答案:

答案 0 :(得分:0)

添加此方法:

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

答案 1 :(得分:0)

事实上你不需要这么做。创建ScrollView。使用更大的图像创建imageview。设置imageview的框架和scrollview的框架类似。使用图像的宽度和高度设置scrollview的内容大小。现在,从scrollview的委托方法,执行以下操作:

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