我有一个视图,并希望UIImage为此视图设置backGroudColor。 代码为:
self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backImage.png"]];
问题是:backImage'frame比视图小。 如何将UIImage拉伸到我的视野。 我知道使用UIImageView可以达到。
有人有好主意吗?更新
我无法上传一张图片。
像这样:backImge的大小是30 * 30,我的视图大小是1024 * 700,
当我使用myview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backImage.png"]];
结果是myview的backGroup有很多'backImage.png'。 我的目标是有一个'backImage.png'充满我的观点。
答案 0 :(得分:117)
试一试。
self.layer.contents = (id)[UIImage imageNamed:@"freshBackground.png"].CGImage;
self.view.layer.contents = UIImage(named: "freshBackground")!.cgImage
答案 1 :(得分:23)
据我所知,您无法直接调整背景图案图像的大小。最简单的方法是更改图像以适合父视图的帧大小。或者,您可以重新绘制图像以适合父视图的大小。
UIView * yourView = [[UIView alloc] initWithFrame:CGRectMake(10.f, 100.f, 300.f, 100.f)];
UIImage * targetImage = [UIImage imageNamed:@"backImage.png"];
// redraw the image to fit |yourView|'s size
UIGraphicsBeginImageContextWithOptions(yourView.frame.size, NO, 0.f);
[targetImage drawInRect:CGRectMake(0.f, 0.f, yourView.frame.size.width, yourView.frame.size.height)];
UIImage * resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[yourView setBackgroundColor:[UIColor colorWithPatternImage:resultImage]];
答案 2 :(得分:3)
world000的答案翻译成Swift:
package org.eclipse.jetty.cookbook;
import java.io.File;
import java.nio.file.Path;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
public class WebAppContextFromFileSystem
{
public static void main(String[] args) throws Exception
{
Server server = new Server(8080);
Path warPath = new File("webapps/hello.war").toPath().toRealPath();
System.err.println("WAR File is " + warPath);
// A very bare bones example (see other examples for
// how to use WebAppContext Configurations to enable
// jndi, annotations, etc ...)
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setWar(warPath.toUri().toASCIIString());
server.setHandler(webapp);
server.start();
server.join();
}
}
答案 3 :(得分:2)
根据Kjuly的回答,如果您希望图像在中心正确插入而不是被拉伸,请使用此方法获取新图像:
#pragma mark - Image
+(UIImage*)imageFitInCenterForSize:(CGSize)inSize forSourceImage:(UIImage*)inImage
{
// redraw the image to fit |yourView|'s size
CGSize imageOriginalSize = inImage.size;
UIImage *resultImage = nil;
if (imageOriginalSize.width<=inSize.width && imageOriginalSize.height<=inSize.height)
{
UIGraphicsBeginImageContextWithOptions(inSize, NO, 0.f);
[inImage drawInRect:CGRectMake((inSize.width-imageOriginalSize.width)/2.0, (inSize.height-imageOriginalSize.height)/2.0, imageOriginalSize.width, imageOriginalSize.height)];
resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return resultImage;
}
与重载变体相同:
+(UIImage*)imageFitInCenterForSize:(CGSize)inSize forSourceImage:(UIImage*)inImage xOffset:(CGFloat)inXOffset yOffset:(CGFloat)inYOffset
{
// http://stackoverflow.com/questions/10629403/ios-colorwithpatternimage-stretch-image-full-screen
// redraw the image to fit |yourView|'s size
CGSize imageOriginalSize = inImage.size;
UIImage *resultImage = nil;
if (imageOriginalSize.width<=inSize.width && imageOriginalSize.height<=inSize.height)
{
UIGraphicsBeginImageContextWithOptions(inSize, NO, 0.f);
[inImage drawInRect:CGRectMake((inSize.width-imageOriginalSize.width)/2.0+inXOffset, (inSize.height-imageOriginalSize.height)/2.0+inYOffset, imageOriginalSize.width, imageOriginalSize.height)];
resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return resultImage;
}
答案 4 :(得分:1)
我使用以下方法:
代码:
UIGraphicsBeginImageContext(self.window.frame.size);
[[UIImage imageNamed:@"background"] drawInRect:self.window.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.window.backgroundColor = [UIColor colorWithPatternImage:image];
答案 5 :(得分:0)
这里基于先前的评论,首先缩放图像,然后按比例填充图像。
func imageScaledToFillSize(size: CGSize, image: UIImage) -> UIImage
{
let aspect = image.size.width / image.size.height;
UIGraphicsBeginImageContextWithOptions(size, false, 0)
if (size.width / aspect <= size.height) {
let resizedImg = self.imageScaledToSize(CGSize(width: size.height * aspect, height: size.height), image: image)
resizedImg.drawInRect(CGRectMake((size.width - resizedImg.size.width)/2, 0, resizedImg.size.width, resizedImg.size.height))
} else {
let resizedImg = self.imageScaledToSize(CGSize(width: size.width, height: size.width / aspect), image: image)
resizedImg.drawInRect(CGRectMake(0, (size.height-resizedImg.size.height)/2, resizedImg.size.width, resizedImg.size.height))
}
let imageR = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return imageR;
}
func imageScaledToSize(size: CGSize, image: UIImage) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, 0.0);
image.drawInRect(CGRectMake(0.0, 0.0, size.width, size.height))
let imageR = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
return imageR;
}