1

这是 iPhoto 中的图像:

在此处输入图像描述

这是 iPhoto 左上角的放大图:

在此处输入图像描述

这是我的应用程序中的相同图像:

在此处输入图像描述

在这里它被放大到我的应用程序的左上角:

在此处输入图像描述

如何丢失图像周围多余的灰色空间并将滚动限制在 UIImage 的边界(如 iPhoto)?

谢谢

4

2 回答 2

0

因此,除了使用 3rd 方库之外,我还使用以下方法解决了这个问题:

IOS。如何将 UIScrollview 滚动限制在有限的范围内?,将答案调整为以下方法,我希望这些方法可以自我解释:

- (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

}
于 2012-12-13T19:22:25.720 回答
0

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

于 2012-12-11T23:06:46.323 回答