iOS:使图像宽度略小于屏幕宽度

标签 ios objective-c

我遇到了图像太大的问题。我需要它略小于屏幕宽度。

这是我的 Controller :

- (void)viewDidLoad {
    [super viewDidLoad];

    self.imageName = @"goldencoaster";

    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"table"]]];

    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", self.imageName]];
    CGRect bounds;
    bounds.origin = CGPointZero;
    bounds.size = [[UIScreen mainScreen] bounds].size;

    self.coasterImage.bounds = bounds;
    self.coasterImage.image = image;
}

但是,当我运行模拟器时,这会返回:

enter image description here

如何让图片显示得比屏幕宽度略小?

============================================= ===================== 更新

所以我按照建议取出了自动布局,并将我的 Controller 更新为如下所示:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.imageName = @"goldencoaster";

    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"table"]]];

    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", self.imageName]];

    CGRect screen = [[UIScreen mainScreen] bounds];

    CGRect bounds;
    bounds.origin.x = screen.origin.x + 10;
    bounds.origin.y = screen.origin.y + 10;
    bounds.size.width = screen.size.width - 100;

    self.coasterImage.frame = bounds;
    self.coasterImage.image = image;
}

但是,现在我的图像根本不显示了??仅显示背景(名称为“table”的图像),但不显示 goldencoaster。

最佳答案

试试这个

- (void)viewDidLoad {
[super viewDidLoad];

self.imageName = @"goldencoaster";

[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"table"]]];

UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", self.imageName]];
CGRect bounds;
bounds.origin = CGPointZero;
bounds.size = [[UIScreen mainScreen] bounds].size;

self.coasterImage.bounds = bounds;
self.coasterImage.image = image;
self.coasterImage.frame = CGRectMake(20, 0,   self.view.frame.size.width-40, self.view.frame.size.height);
self.coasterImage.center = self.coasterImage.superview.center;

}

swift 3.1

override func viewDidLoad() {
    super.viewDidLoad()

    imageName = "goldencoaster"

    view.backgroundColor = UIColor(patternImage: UIImage(named: "table")!)

    let image = UIImage(named: imageName)
    var bounds = CGRect()
    bounds.origin = CGPoint.zero
    bounds.size = (UIScreen.main.bounds).size

    coasterImage.bounds = bounds
    coasterImage.image = image
    coasterImage.frame = CGRect(x: CGFloat(20), y: 0, width: self.view.frame.size.width - CGFloat(40), height: self.view.frame.size.height)
    coasterImage.center = (coasterImage.superview?.center)!
}

关于iOS:使图像宽度略小于屏幕宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32287354/

相关文章:

iphone - 尝试 iPhone 上的小型 winy 3D 类(class)

objective-c - NSThread 和对象保留计数问题

objective-c - iOS中的音频控制

ios - 如何一个接一个地呈现多个 UIAlertController?

ios - 如何设置 SKProductsRequest 超时秒? SKProductsRequest 的默认超时秒数是多少?

objective-c - ARC IBOutlet 存储类型 iOS 限制

objective-c - 将 NSLocalizedRecoveryOptionsErrorKey 设置为 NSError UserInfo 不提供任何恢复选项

IOS——如何在 UITableViewCell 中移动 UILabel?

ios - 如何为 Storyboard 创建 Xml?

objective-c - 为 Web 浏览器到 native 应用程序切换设置 apple-app-site-association (iOS 8)