在iOS上使用Cocoa,在后台进行9切片缩放/拉伸

时间:2011-06-17 13:27:47

标签: iphone ios cocoa-touch ipad

我想使用“9切片”缩放来拉伸图像,其中仅缩放图像的中心。完全像UIImage的stretchableImageWithLeftCapWidth方法,但我想在后台线程中执行此操作。据我所知,这个方法只能在gui线程中使用。

我正在开发iOS / iPad / iPhone。

有没有人有代码段,或者知道可以执行此操作的库?我正试图不重新发明轮子!

我发现一些有用的参考资料:

CGContext docs:

关于在后台绘图的精彩博客条目:

使用CGContext旋转图像:

2 个答案:

答案 0 :(得分:3)

从iOS 4开始,UIKit的某些部分是线程安全的;我相信UIGraphics函数和UIImage绘图应该可以工作。

如果你需要支持iOS 3,那就有点困难了。如果你想完全避免UIKit(可能是明智的),那么你需要将后台线程传递给CGImageRef。还有一些你可能需要担心的事情;我假设scale为1,imageOrientation为肖像。

  1. 创建上下文:CGBitmapContextCreate()
  2. 创建子图像:CGImageCreateWithImageInRect()
  3. 将每个子图像绘制到正确的位置:CGContextDrawImage()
  4. 从上下文中获取图片:CGBitmapContextCreateImage()
  5. 释放必要的东西并将CGImageRef返回到主线程。
  6. 在主线程中,使用+[UIImage imageWithCGImage:]+imageWithCGImage:scale:orientation:(请注意,在后一种情况下,带有比例2的图像似乎只支持纵向方向;我不知道为什么)。
  7. 另请注意,将图像粘贴在UIImageView中并适当设置contentStretch会更好,因为此时缩放是在GPU上完成的。

答案 1 :(得分:1)

AppKit有一个很好的NSDrawNinePartImage()功能。我在网上找到了这个代码,它为iOS提供了类似的功能。

http://pastie.org/1147077/

//
//  UIImageAdditions.m
//  Newspress
//
//  Created by Kyle Van Essen on 10-04-10.
//  Copyright 2010 Vibealicious. All rights reserved.
//

#import "UIImageAdditions.h"


@implementation UIImage (Additions)

+(void)drawNinePartImage:(NSArray *)images inRect:(CGRect)rect
{
    CGPoint origin = rect.origin;
    CGSize size = rect.size;

    NSInteger partCount = 9;

    if ([images count] < partCount)
        return;

    CGRect rects[partCount];
    UIImage *image;

    // Top Row
    image = [images objectAtIndex:0];
    rects[0] = CGRectMake(origin.x, origin.y, image.size.width, image.size.height);

    image = [images objectAtIndex:2];
    rects[2] = CGRectMake(origin.x + size.width - image.size.width, origin.y, image.size.width, image.size.height);

    image = [images objectAtIndex:1];
    rects[1] = CGRectMake(rects[0].size.width + rects[0].origin.x, origin.y, size.width - rects[0].size.width - rects[2].size.width, image.size.height);

    // Bottom Row
    image = [images objectAtIndex:6];
    rects[6] = CGRectMake(origin.x, origin.y + size.height - image.size.height, image.size.width, image.size.height);

    image = [images objectAtIndex:8];
    rects[8] = CGRectMake(origin.x + size.width - image.size.width, origin.y + size.height - image.size.height, image.size.width, image.size.height);

    image = [images objectAtIndex:7];
    rects[7] = CGRectMake(rects[6].size.width + rects[6].origin.x, origin.y + size.height - image.size.height, size.width - rects[6].size.width - rects[8].size.width, image.size.height);

    // Middle Row
    image = [images objectAtIndex:3];
    rects[3] = CGRectMake(origin.x, origin.y + rects[0].size.height, image.size.width, size.height - rects[0].size.height - rects[6].size.height);

    image = [images objectAtIndex:5];
    rects[5] = CGRectMake(origin.x + size.width - image.size.width, origin.y + rects[0].size.height, image.size.width, size.height - rects[2].size.height - rects[8].size.height);

    image = [images objectAtIndex:4];
    rects[4] = CGRectMake(rects[3].size.width + rects[3].origin.x, origin.y + rects[0].size.height, size.width - rects[0].size.width - rects[2].size.width, size.height - rects[1].size.height - rects[7].size.height);

    for (NSInteger index = 0; index < partCount; index++)
    {               
        UIColor *pattern = [[UIColor alloc] initWithPatternImage:[images objectAtIndex:index]];

        //NSLog(@"Frame: %f, %f, %f, %f", rects[index].origin.x, rects[index].origin.y, rects[index].size.width, rects[index].size.height);

        [pattern set];
        CGContextSetPatternPhase(UIGraphicsGetCurrentContext(), CGSizeMake(rects[index].origin.x, rects[index].origin.y));
        UIRectFill(rects[index]);

        [pattern release];
    }

    [[UIColor clearColor] set];
}

@end