2011-07-08 69 views
0

我想中心的圖像(相對於iPhone屏幕),使得當視圖加載時圖像是中心寬度/高度...在我的情況下,我的視圖加載在圖像本身的像素點0,0,如果我縮小圖像出現在左上角,而不是屏幕/視圖的中心。以下是我迄今爲止:如何以uiimageview/uiscrollview居中圖像(前後縮放)

UIImage *image = [UIImage imageNamed: @"t.bmp"]; 
self.view.backgroundColor = [UIColor blackColor]; 
self.mi.frame = CGRectMake(0, 0, image.size.width, image.size.height); 
self.mi.contentMode = (UIViewContentModeCenter); 
self.mi.backgroundColor = [UIColor blackColor]; 
[self.mi setImage:image]; 

/* Draw a line here!! 
UIGraphicsBeginImageContext(self.mi.frame.size); 
[self.mi.image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)]; 
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); 
CGContextSetLineWidth(context, 20.0); 
CGContextBeginPath(context); 
CGContextMoveToPoint(context, 0,0); 
CGContextAddLineToPoint(context, 200, 200);  
CGContextMoveToPoint(context, 200,0); 
CGContextAddLineToPoint(context, 0, 200); 
CGContextStrokePath(context); 
self.mi.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
*/ 

self.expimg.maximumZoomScale = 2.0; 
self.expimg.minimumZoomScale = 0.1; 
self.expimg.clipsToBounds = YES; 
self.expimg.delegate = self; 
[self.expimg setContentSize:self.mi.frame.size]; 

[self.expimg addSubview: self.mi]; 
[self.view addSubview: self.expimg]; 

謝謝!

編輯:mi是UIImageView,expimg是UIScrollView(使用的接口生成器)。另外,定義了viewForZoomingInScrollView。

回答

1

您應該可以通過調整圖像的位置和大小以及在初始加載時滾動視圖以及縮放級別進行更改。制定一個在viewDidLoad上調用的方法,並再次在UIScrollViewDelegate方法scrollViewDidZoom:上調用。

像這樣的東西應該工作(其中的ImageView和滾動視圖是類屬性):

- (void)centerImage { 

    CGSize screenSize = CGSizeMake(320, 480); 

    CGSize imageSize = imageView.frame.size; 
    imageSize.width = imageSize.width * scrollView.zoomScale; 
    imageSize.height = imageSize.height * scrollView.zoomScale; 
    CGSize scrollSize = scrollView.frame.size; 

    CGFloat centerX; 
    CGFloat centerY; 
    CGFloat left; 
    CGFloat top; 

    if (imageSize.width > screenSize.width) { 
     scrollSize.width = imageSize.width; 
     centerX = imageSize.width/2; 
     left = 0; 
    } else { 
     scrollSize.width = screenSize.width; 
     centerX = screenSize.width/2; 
     left = centerX - imageSize.width/2; 
    } 

    if (imageSize.height > screenSize.height) { 
     scrollSize.height = imageSize.height; 
     centerY = imageSize.height/2; 
     top = 0; 
    } else { 
     scrollSize.height = screenSize.height; 
     centerY = screenSize.width/2; 
     top = centerY - imageSize.height/2; 
    } 

    CGRect scrollFrame = scrollView.frame; 
    scrollFrame.size = scrollSize; 
    scrollView.frame = scrollFrame; 

    CGRect imageFrame = imageView.frame; 
    imageFrame.origin = CGPointMake(left, top); 

    scrollView.contentOffset = CGPointMake(centerX-(imageSize.width/2), centerY-(imageSize.height/2)); 

} 

我沒有測試過這一點,所以我的一些數學可能是錯誤的。無論哪種方式,它都希望能給你一個很好的做法。

+0

您的解決方案不工作,但我找到了答案:http://stackoverflow.com/questions/638299/uiscrollview-with-centered-uiimageview-like-照片 - 應用程序 順便說一句,在我的意見是正確的方式來畫一條線?它的工作只是想確保 – Alede

-1

[scrollView zoomToRect:CGRectMake(x,y,width,height)animated:YES];將爲你工作... 在哪裏矩形應該有這些中心座標爲原點。

0

第1步:創建隨後的形象圖酒店在viewDidLoad中初始化它,並添加到滾動視圖:

self.pictureImage = [[UIImageView alloc]initWithImage:self.picture]; 
[_scrollView addSubview:_pictureImage]; 
self.pictureImage.alpha = 0; 
_scrollView.delegate = self; 

第2步:創建adjustZoomScale函數來獲取最小規模

- (CGFloat)adjustZoomScale{ 
CGFloat scale = self.scrollView.bounds.size.width/self.pictureImage.bounds.size.width; 
CGFloat h = scale * self.pictureImage.bounds.size.height; 
if (h > self.scrollView.bounds.size.height) { 
    scale = self.scrollView.bounds.size.height/self.pictureImage.bounds.size.height; 
} 
return scale; 
} 

步驟3:由於滾動視圖的框架將更新爲viewDidAppear因此我們需要在此調整縮放比例。並設置阿爾法使圖像顯得更光滑:

- (void)viewDidAppear:(BOOL)animated { 
[super viewDidAppear: animated]; 
_scrollView.maximumZoomScale = 1.0; 
_scrollView.minimumZoomScale = [self adjustZoomScale]; 
[_scrollView setZoomScale:_scrollView.minimumZoomScale]; 
self.pictureImage.alpha = 1; 
} 

步驟4:實施viewForZoomingInScrollView(滾動委託方法),並返回圖像視圖

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

步驟5:實施scrollViewDidZoom(滾動委託方法)和調整點圖像視圖

- (void)scrollViewDidZoom: (UIScrollView*) scrollView { 
CGSize boundsSize = scrollView.bounds.size; 
CGRect contentsFrame = _pictureImage.frame; 

if (contentsFrame.size.width < boundsSize.width) { 
    contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width)/2.0; 
} else { 
    contentsFrame.origin.x = 0.0; 
} 

if (contentsFrame.size.height < boundsSize.height) { 
    contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height)/2.0; 
} else { 
    contentsFrame.origin.y = 0.0; 
} 
_pictureImage.frame = contentsFrame; 
} 

謝謝shawhuthis post的點調整圖像視圖縮放時