9-slice background scaling / stretching using Cocoa on iOS

I want to stretch the image using 9-slice scaling, where only the center of the image is scaled. Just like the stretchableImageWithLeftCapWidthUIImage method , I want to do this in a background thread. I understand that this method can only be used in the gui thread.

I am developing on iOS / iPad / iPhone.

Does anyone have a piece of code or know a library that can do this? I try not to reinvent the wheel!

Some related links that I found useful:

CGContext docs:

A good blog post in the figure in the background:

Rotate an image using a CGContext:

+5
source share
2 answers

Since iOS 4, some parts of UIKit are thread safe; I believe that the UIGraphics functions and the UIImage drawing should work.

If you need to support iOS 3, then this is a little trickier. If you want to completely exclude UIKit (maybe wise), you need to pass the background thread to CGImageRef. There are additional things you may need to worry about; I assume it scaleis 1, and imageOrientationa portrait.

  • Create context: CGBitmapContextCreate()
  • Create your own sub-image: CGImageCreateWithImageInRect().
  • Draw each sub-image in the right place: CGContextDrawImage()
  • : CGBitmapContextCreateImage()
  • CGImageRef .
  • +[UIImage imageWithCGImage:] +imageWithCGImage:scale:orientation: ( , 2, , , , ).

, UIImageView contentStretch , .

+3

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
+1

All Articles