[英]ios: colorWithPatternImage , stretch image full screen
我有一個視圖,並希望通過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'充滿我的觀點。
試試。
self.layer.contents = (id)[UIImage imageNamed:@"freshBackground.png"].CGImage;
self.view.layer.contents = UIImage(named: "freshBackground")!.cgImage
據我所知,你無法直接調整背景的圖案圖像。 最簡單的方法是更改圖像以適合父視圖的幀大小。 或者,您可以重繪圖像以適合父視圖的大小。
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]];
world000的答案翻譯成Swift:
self.layer.contents = UIImage(named: "freshBackground.png")?.CGImage
根據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;
}
我使用以下方法:
碼:
UIGraphicsBeginImageContext(self.window.frame.size);
[[UIImage imageNamed:@"background"] drawInRect:self.window.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.window.backgroundColor = [UIColor colorWithPatternImage:image];
這是基於之前的評論,一個版本首先縮放圖像,然后裁剪成比例填充。
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.