colworWithPatternImage返回1像素透明边框

时间:2016-08-25 09:30:43

标签: ios objective-c uiimageview uiimage

我正在尝试使用平铺图案创建图像,但平铺图像似乎有1像素透明边框。即使我只使用白色背景图像,我仍然可以获得透明边框。

// Preparation
let rawString = "from John Smith on 25.08. at 8:00"
let attributedString = NSMutableAttributedString(string: rawString)
let nameRange = (rawString as NSString).rangeOfString("John Smith")
let italicFont = UIFont.italicSystemFontOfSize(14)

// Make entire string italic: works!
attributedString.addAttributes([NSFontAttributeName : italicFont], range: NSMakeRange(0, 33))
// Make the name string additionally bold: doesn't work!
attributedString.addAttributes([UIFontDescriptorTraitsAttribute:
        [UIFontWeightTrait: NSNumber(double: Double(UIFontWeightBold))]], range: nameRange)

// Show it on the label
attributedStringLabel.attributedText = attributedString

图案图片 pattern image

带有透明1px边框的平铺图像 tiled image

使用双三次重采样从Photoshop导出模式图像,但我也尝试了双线性和最近邻方法。此外,如果我在Photoshop中使用图像作为图案,我不会得到透明边框,这使我认为它与iOS有关。

1 个答案:

答案 0 :(得分:0)

-(UIImage*) resizeImage:(UIImage*)image newSize:(CGSize)newSize
{
        UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
        [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();     
        return newImage;
}

我忘了在我的问题中包含我的辅助方法,它会缩放平铺图像。正是这种方法给了我这个问题。当我按像素增加宽度和高度时,我不再在返回的图像中获得透明边框。

-(UIImage*) resizeImage:(UIImage*)image newSize:(CGSize)newSize
{
        UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
        [image drawInRect:CGRectMake(0, 0, newSize.width+1, newSize.height+1)];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();       
        return newImage;

}