iPhone:如何仅从scrollview缩放图像
我在ScrollView缩放方面有这个问题。有很多关于它的文章,但不知何故,我找不到合适的解释、教程或代码片段 我有全屏滚动视图和全屏图像视图。在imageview内部,我加载AspectFit图像(它们可以是垂直的,也可以是水平的,水平的图像上下都有黑色空间)。当我进行缩放时,我不仅缩放图像,而且缩放周围的区域(imageview的其余部分)。此外,当我在缩放时旋转模拟器时,图像会转到右上角,而不是像开始时那样居中。甚至很少有像素从屏幕上消失 我真的希望只缩放图像而不是整个imageview,或者至少在缩放时图像始终居中iPhone:如何仅从scrollview缩放图像,iphone,xcode,image,uiscrollview,Iphone,Xcode,Image,Uiscrollview,我在ScrollView缩放方面有这个问题。有很多关于它的文章,但不知何故,我找不到合适的解释、教程或代码片段 我有全屏滚动视图和全屏图像视图。在imageview内部,我加载AspectFit图像(它们可以是垂直的,也可以是水平的,水平的图像上下都有黑色空间)。当我进行缩放时,我不仅缩放图像,而且缩放周围的区域(imageview的其余部分)。此外,当我在缩放时旋转模拟器时,图像会转到右上角,而不是像开始时那样居中。甚至很少有像素从屏幕上消失 我真的希望只缩放图像而不是整个imageview,
欢迎来自PhotoScroller示例的任何帮助。;将此添加到布局子视图:
// 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;
//当图像小于屏幕大小时,将图像居中
CGSize boundsSize=self.bounds.size;
CGRect frameToCenter=imageView.frame;
//水平居中
if(frameToCenter.size.width
这无助于仅缩放图像,但至少在缩放图像时,它会保持图像居中。来自PhotoScroller示例;将此添加到布局子视图:
// 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;
//当图像小于屏幕大小时,将图像居中
CGSize boundsSize=self.bounds.size;
CGRect frameToCenter=imageView.frame;
//水平居中
if(frameToCenter.size.width
这对只缩放图像没有帮助,但至少在缩放图像时,它会使图像居中。我解决了这个问题,这不是最好的代码,但目前对我来说效果很好,我可能稍后会清理它。在这里发布,可能会对某人有所帮助:)
UIImage*img=[[UIImage alloc]initwithcontentsoff文件:[[NSBundle mainBundle]
pathForResource:someImage的类型:@“jpg”]];
CGFloat标度;
if(scrollView.frame.size.widthscrollView.frame.size.height)//横向
比例=scrollView.frame.size.height/img.size.height;
imgView.frame=CGRectMake(scrollView.frame.origin.x,scrollView.frame.origin.y,
外形尺寸、宽度*比例、外形尺寸、高度*比例);
imgView.center=CGPointMake(scrollView.frame.size.width/2,scrollView.frame.size.height/2);
[imgView setImage:img];
[滚动视图设置缩放比例:缩放];
我解决了这个问题,它不是最好的代码,但目前对我来说效果很好,我可能稍后会清理它。在这里发布,可能会对某人有所帮助:)
UIImage*img=[[UIImage alloc]initwithcontentsoff文件:[[NSBundle mainBundle]
pathForResource:someImage的类型:@“jpg”]];
CGFloat标度;
if(scrollView.frame.size.widthscrollView.frame.size.height)//横向
比例=scrollView.frame.size.height/img.size.height;
imgView.frame=CGRectMake(scrollView.frame.origin.x,scrollView.frame.origin.y,
外形尺寸、宽度*比例、外形尺寸、高度*比例);
imgView.center=CGPointMake(scrollView.frame.size.width/2,scrollView.frame.size.height/2);
[imgView setImage:img];
[滚动视图设置缩放比例:缩放];
我应该把它放在哪里?:)我只是用imgView.image=[UIImage ImageName:whichNext]更改图像;我并没有用图像初始化任何矩形。我只是更改了imageView的内容。您可以将其放在layoutSubviews中-如果您在创建UIImageView时不初始化它,它可以采用您初始化它时使用的图像的大小(但是如果您给它一个具有新大小的不同图像,您应该调用[sizeToFit]
来调整它的大小)。正如你在上面看到的,在这种情况下,你也不会改变它的大小;您只是在更改其原点,以便在缩小时将其自身居中。我应该将其放置在何处?:)我只是用imgView.image=[UIImage ImageName:whichNext]更改图像;我并没有用图像初始化任何矩形。我只是更改了imageView的内容。您可以将其放在layoutSubviews中-如果您在创建UIImageView时不初始化它,它可以采用您初始化它时使用的图像的大小(但是如果您给它一个具有新大小的不同图像,您应该调用[sizeToFit]
来调整它的大小)。正如你在上面看到的,在这种情况下,你也不会改变它的大小;您只需更改其原点,以便在缩小时将其自身居中。