簡體   English   中英

使UIImageView圖像略小

[英]Make UIImageView image slightly smaller

我想使UIImageView中的圖像比最初為我設置的圖像小一些。

我已經將contentMode發送到UIViewContentModeScaleAspectFill,但是我仍然想使其更小一些。

self.imgCamera.layer.cornerRadius = self.imgCamera.frame.size.width / 2;
self.imgCamera.contentMode=UIViewContentModeScaleAspectFill;
self.imgCamera.clipsToBounds = YES;

假設您從情節提要中的出路是

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

在您的編碼內部,使用它來使UIImageView成為“填充”,這應該使圖像大小小於框架的結果。

imageView.bounds = CGRectInset(imageView.frame, 10.0f, 10.0f);

您可以創建實用程序方法:

- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    //UIGraphicsBeginImageContext(newSize);
    // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
    // Pass 1.0 to force exact pixel size.
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return newImage;
}

並實現它:

UIImage *myIcon = [self imageWithImage:myImage scaledToSize:CGSizeMake(20, 20)]

希望這會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM