Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 点击UIScrollView时,UIImageView会自动调整大小_Objective C_Uiscrollview_Uiimageview - Fatal编程技术网

Objective c 点击UIScrollView时,UIImageView会自动调整大小

Objective c 点击UIScrollView时,UIImageView会自动调整大小,objective-c,uiscrollview,uiimageview,Objective C,Uiscrollview,Uiimageview,好的,我在UIScrollView中有一个UIImageView。我将图像动态加载到UIImageView中,然后手动调整其大小。到现在为止,一直都还不错。但是,如果我触摸UIScroll scroll view(包含调整大小的元素),所有内容都会调整回原始大小。以防万一,下面是调整图像大小的代码: /** * Proportionally resize and position doodle */ - (void)resizeDoodle:(CGFloat)height { self.do

好的,我在UIScrollView中有一个UIImageView。我将图像动态加载到UIImageView中,然后手动调整其大小。到现在为止,一直都还不错。但是,如果我触摸UIScroll scroll view(包含调整大小的元素),所有内容都会调整回原始大小。以防万一,下面是调整图像大小的代码:

 /**
* Proportionally resize and position doodle
*/
- (void)resizeDoodle:(CGFloat)height {

self.doodleImageView.frame = CGRectMake(0, 0, self.view.bounds.size.width, height);
[self.scrollView setContentSize:self.doodleImageView.frame.size];
}
这条线保持图像的一致性

self.Imageview.contentMode = UIViewContentModeScaleAspectFit;
写上面的代码来限制你的问题

另一种方式:

对于特定的(您需要的)图像使用以下方法

+ (UIImage*)resizeImage:(UIImage*)image withWidth:(int)width withHeight:(int)height
{
    CGSize newSize = CGSizeMake(width, height);
    float widthRatio = newSize.width/image.size.width;
    float heightRatio = newSize.height/image.size.height;

    if(widthRatio > heightRatio)
    {
        newSize=CGSizeMake(image.size.width*heightRatio,image.size.height*heightRatio);
    }
    else
    {
        newSize=CGSizeMake(image.size.width*widthRatio,image.size.height*widthRatio);
    }


    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}
此方法返回NewImage,具体大小由您指定:)

此代码可能对您有所帮助:)

谢谢您的回复。这确实迫使图像按比例调整大小,但并没有解决我的问题。在我尝试滚动UIScrollView后,图像仍然恢复到原始大小。谢谢。我当然可以调整图像的大小,但由于某些原因,它会在我滚动后缩小。我不知道它为什么会这样做,尤其是在成功调整大小后。self.View.autoresizingMask=UIViewAutoresizingFlexibleWidth | uiviewAutoresizingflexiblewhight;。。。请将此代码用于您的view@AndreiTaraschuk可能是,需要为该viewcomponent禁用Autolayout。因为Autolayout强制将其重新调整为相同大小。
+ (UIImage*)resizeImage:(UIImage*)image withWidth:(int)width withHeight:(int)height
{
    CGSize newSize = CGSizeMake(width, height);
    float widthRatio = newSize.width/image.size.width;
    float heightRatio = newSize.height/image.size.height;

    if(widthRatio > heightRatio)
    {
        newSize=CGSizeMake(image.size.width*heightRatio,image.size.height*heightRatio);
    }
    else
    {
        newSize=CGSizeMake(image.size.width*widthRatio,image.size.height*widthRatio);
    }


    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}