如何加载图像已经缩小

时间:2013-04-15 03:20:43

标签: iphone objective-c image zoom zooming

我想在我的iPhone应用中显示一个大图像。它的行为有点奇怪,我不能把它看得那么大。有没有办法加载它按比例缩小,以便整个图像适合屏幕,然后你可以放大?

- (void)loadImageView1 {
    KFImageZoomView *image = [[KFImageZoomView alloc] initWithImageNamed:@"TrappsTrailMapImage.png"atLocation:CGPointMake(0,0)];
    [self addSubview:image];
    [image release];
}

自定义类:

.h文件

#import <UIKit/UIKit.h>

@interface KFImageZoomView : UIScrollView <UIScrollViewDelegate> {
    UIImageView *imageView;
}

@property (nonatomic, retain)   UIImageView     *imageView;

#pragma mark Initialisation
- (id)initWithImageNamed:(NSString *)imageName atLocation:(CGPoint)location;

@end

.m文件

#import "KFImageZoomView.h"

@implementation KFImageZoomView

@synthesize imageView;

#pragma mark - Memory Management

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

#pragma mark - Initialisation

- (id)initWithImageNamed:(NSString *)imageName atLocation:(CGPoint)location {
    self = [super init];
    if (self) {
        // Assign the delegate
        self.delegate = self;

        // Create our image view using the passed in image name
        self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];

        // Update the ImageZoom frame to match the dimensions of passed in image
        self.frame = CGRectMake(location.x, location.y, 
                                self.imageView.frame.size.width, self.imageView.frame.size.height);

        // Set a default minimum and maximum zoom scale
        self.minimumZoomScale = 0.01f;
        self.maximumZoomScale = 10.0f;

        // Add image view as a subview
        [self addSubview:self.imageView];
    }

    return self;
}

#pragma mark - UIScrollViewDelegates

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {

}

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


@end

0 个答案:

没有答案