UIImageView change Width and Height

后端 未结 8 786
-上瘾入骨i
-上瘾入骨i 2021-02-13 10:12

I\'ve read various posts on here asking similar questions... I\'ve tried various ways that were posted including bounds and frames etc. including the following:



        
相关标签:
8条回答
  • 2021-02-13 11:10

    First off, you can't set the frame or bounds of the UIImage - that will only work on a UIImageView.

    I've found that changing the frame of a UIImageView causes the Image to be scaled to the new size. Sometimes, that's undesirable - and you want instead to crop the image.

    I can't tell if this is what you're asking for, but here's some code to crop an image to a specific size in a UIImageView:

    UIImage *myImage = [UIImage imageNamed:@"photo.png"];
    
    CGRect cropRect = CGRectMake(0.0, 0.0, 320.0, 44.0));
    CGImageRef croppedImage = CGImageCreateWithImageInRect([myImage CGImage], cropRect);
    
    UIImageView *myImageView = [[UIImageView alloc] initWithFrame:cropRect];
    [myImageView setImage:[UIImage imageWithCGImage:croppedImage]]; 
    
    CGImageRelease(croppedImage);
    
    0 讨论(0)
  • 2021-02-13 11:13

    The following will change the size of the UIImaveView, clipping the underlying image without resizing it and keeping it aligned to bottom left of view:

    imageView.frame = CGRectMake(
                 imageView.frame.origin.x, 
                 imageView.frame.origin.y, newWidth, newHeight);
    
    imageView.contentMode = UIViewContentModeBottomLeft; // This determines position of image
    imageView.clipsToBounds = YES;
    
    0 讨论(0)
提交回复
热议问题