我以編程方式在視圖控制器上添加了uiview作爲子視圖(稱爲contentView)。我還以編程方式在該uiview上添加了一個圖像。每次我在iPad上運行應用程序時,圖像都被拉長了!如何修復圖像,使其適合iPad屏幕,但不會拉伸圖像?我知道drawInRect
是拉伸形象。那麼我該如何解決這個問題?如何以編程方式使用uiview上的圖像
這是我的代碼:
UIGraphicsBeginImageContextWithOptions(self.contentView.bounds.size, self.contentView.opaque, 0.0);
[[UIImage imageNamed:@"menu image 2.JPG"] drawInRect:self.contentView.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.contentView addSubview:imageView];
沒有任何理由對這種複雜的圖像assignement? '[[UIImageView alloc] initWithImage:[UIImage imageNamed:@「menu image 2.JPG」]];' - 這應該是所有需要的。爲此創建新的圖像上下文似乎是一種矯枉過正。 – Losiowaty