UIImageView change Width and Height

后端 未结 8 791
-上瘾入骨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);
    

提交回复
热议问题