如何将UIImageView的滚动限制为像iPhoto中的UIImage的边界?

时间:2012-12-11 23:00:57

标签: objective-c ios uiscrollview uiimageview uiimage

这是iPhoto中的图像:

enter image description here

这是放大到iPhoto的左上角:

enter image description here

以下是我的应用中的相同图片:

enter image description here

此处它放大到我的应用中的左上角:

enter image description here

如何丢失图像周围多余的灰色空间,并将滚动限制为像iPhoto一样的UII图像边界?

由于

2 个答案:

答案 0 :(得分:0)

您可以使用可以执行此操作的库。试试MWPhotoBrowser

答案 1 :(得分:0)

因此,除了使用第三方库外,我使用以下方法解决了这个问题:

iOS. How do I restrict UIScrollview scrolling to a limited extent?,使答案适应以下方法,我希望自我解释:

- (CGRect) methodThatGetsImageSizeOnScreen
{

float frameHeight;
float frameWidth;
float frameXOrigin;
float frameYOrigin;

float threshold;
BOOL thisImageTouchesLeftAndRight;

UIInterfaceOrientation thisOrientation = self.interfaceOrientation;

if (UIInterfaceOrientationIsLandscape(thisOrientation)){

    threshold = 748.0/1024.0;

    if ((self.imageToPresent.size.height == self.imageToPresent.size.width) | ((self.imageToPresent.size.height/self.imageToPresent.size.width) > threshold)){

        thisImageTouchesLeftAndRight = NO;

        frameWidth = (748/self.imageToPresent.size.height)*self.imageToPresent.size.width;
        frameHeight = 748;
        frameXOrigin = (1024-frameWidth)/2;
        frameYOrigin = 0;
    }
    else
    {
        thisImageTouchesLeftAndRight = YES;

        frameWidth = 1024;
        frameHeight = (1024/self.imageToPresent.size.width)*self.imageToPresent.size.height;
        frameXOrigin = 0;
        frameYOrigin = (748-frameHeight)/2;
    }
}
else {

    threshold = 768.0/1004.0;

    if ((self.imageToPresent.size.height == self.imageToPresent.size.width) | ((self.imageToPresent.size.width/self.imageToPresent.size.height) > threshold)){

        thisImageTouchesLeftAndRight = YES;

        frameWidth = 768;
        frameHeight = (768/self.imageToPresent.size.width)*self.imageToPresent.size.height;
        frameXOrigin = 0;
        frameYOrigin = (1004-frameHeight)/2;
    }
    else
    {
        thisImageTouchesLeftAndRight = NO;

        frameWidth = (1004/self.imageToPresent.size.height)*self.imageToPresent.size.width;
        frameHeight = 1004;
        frameXOrigin = (768-frameWidth)/2;
        frameYOrigin = 0;
    }


}

CGRect theRect = CGRectMake(frameXOrigin, frameYOrigin, frameWidth, frameHeight);

return theRect;
}

#pragma mark - UIScrollViewDelegate

- (void) scrollViewDidScroll:(UIScrollView*)scroll{

UIInterfaceOrientation thisOrientation = self.interfaceOrientation;

float largeDimension;
float smallDimension;

if (UIInterfaceOrientationIsLandscape(thisOrientation)){

    largeDimension = 1024;
    smallDimension = 748;
}
else{

    largeDimension = 1004;
    smallDimension = 768;

}

CGPoint offset = scroll.contentOffset;

CGRect results = [self methodThatGetsImageSizeOnScreen];


float frameHeight = results.size.height;
float frameYOrigin = results.origin.y;


float frameWidth = results.size.width;
float frameXOrigin = results.origin.x;


//So, we start the limiting of a landscape image in portrait (in the y direction) when we exceed the following criteria:

if((frameHeight*self.scrollView.zoomScale) > largeDimension){

    if(offset.y < self.scrollView.zoomScale*frameYOrigin) offset.y = self.scrollView.zoomScale*frameYOrigin;

    if(offset.y > ((self.scrollView.zoomScale*frameYOrigin)+(frameHeight*self.scrollView.zoomScale)-largeDimension)) offset.y = ((self.scrollView.zoomScale*frameYOrigin)+(frameHeight*self.scrollView.zoomScale)-largeDimension);

}

if((frameWidth*self.scrollView.zoomScale) > largeDimension){

    if(offset.x < self.scrollView.zoomScale*frameXOrigin) offset.x = self.scrollView.zoomScale*frameXOrigin;

    if(offset.x > ((self.scrollView.zoomScale*frameXOrigin)+(frameWidth*self.scrollView.zoomScale)-largeDimension)) offset.x = ((self.scrollView.zoomScale*frameXOrigin)+(frameWidth*self.scrollView.zoomScale)-largeDimension);

}

    // Set offset to adjusted value
    scroll.contentOffset = offset;

//Remember you may want your minimum zoomScale set in viewDidLoad or viewWillAppear

}