0

I'm programming on objective-c. I have an image a line (see below) (1 x 30) pixels.

enter image description here

How can I get a UIImage (50 x 30) (see below) from this line?

enter image description here

Sveta
  • 1,270
  • 3
  • 16
  • 33
  • See this [thread](http://stackoverflow.com/q/3770591/893863). I hope it be useful for you! – Hamed Rajabi Varamini Nov 22 '11 at 05:31
  • What types of shapes will you be drawing? Do you want this to draw dynamically with touch input from the user? If not...In your example, if your line (1x30) has no gradient to it, you can scale it with setting the content type on the UIImageView. imageView.contentMode = UIViewContentModeScaleToFill; It will not return a UIImage, but will appear how you desire – Jesse Black Nov 22 '11 at 05:33
  • The shape is a rectangle. Line has a gradient. – Sveta Nov 22 '11 at 05:56

4 Answers4

0

Create a CGBitmapContext with size of 50 * 30 than you can just draw that image on the context by using CGContextDrawImage.

After that use CGBitmapContextCreateImage and [UIImage imageWithCGImage:] to create the UIImage

CGContextRef CreateBitmapContext(int pixelsWide, int pixelsHigh)
{
    CGContextRef    context = NULL;
    CGColorSpaceRef colorSpace;
    int             bitmapByteCount;
    int             bitmapBytesPerRow;

    bitmapBytesPerRow   = (pixelsWide * 4); // RGBA
    bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);

    colorSpace = CGColorSpaceCreateDeviceRGB();
    context = CGBitmapContextCreate (NULL,
                                     pixelsWide,
                                     pixelsHigh,
                                     8,      // bits per component
                                     bitmapBytesPerRow,
                                     colorSpace,
                                     kCGImageAlphaPremultipliedLast);
    NSCAssert(context != NULL, @"cannot create bitmap context");
    CGColorSpaceRelease( colorSpace );

    return context;
}

CGContextRef context = CreateBitmapContext(50, 30);
UIImage *yourLineImage = ...;
CGImageRef cgImg = [yourLineImage CGImage];
for (int i = 0; i < 50; i++) {
    CGRect rect;
    rect.origin.x = i;
    rect.origin.y = 0;
    rect.size.width = 1;
    rect.size.height = 30;
    CGContextDrawImage(context, rect, cgImg);
}
CGImageRef output = CGBitmapContextCreateImage(context);
UIImage *result = [UIImage imageWithCGImage:output];
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
  • > Create a CGBitmapContext with size of 50 * 30 than you can just draw that image on the context by using CGContextDrawImage. How can I do it? Help me, please. I don't know C. – Sveta Nov 22 '11 at 05:47
  • @Sveta add some more code, if you want to know more about 2D drawing you should read some Quartz2D doc http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/Introduction/Introduction.html#//apple_ref/doc/uid/TP30001066 – Bryan Chen Nov 22 '11 at 06:24
0

If you just want to draw the image, you might want to try UIImage's drawInRect: method.

You'd typically want to call this from your custom UIView's drawRect:.

There are different approaches to drawing in Cocoa (and Cocoa-Touch) so here's Apple's Drawing and Printing Guide for iOS.

0

if your line has simple color, try this lazy method:

UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 30)];
[line setImage:[UIImage imageNamed:@"your gray line"]];
[self.view addSubView:line];
SentineL
  • 4,682
  • 5
  • 19
  • 38
  • I don't need a UIImageView. I need UIImage for UIButton. – Sveta Nov 22 '11 at 05:52
  • then you'll need to do something like xlc0212 posted. If you don't know what to do with what he talkink about, I'll post another solution – SentineL Nov 22 '11 at 05:59
  • OMG! how didn't i think about it earlyer?! just set your 1x30 image as gackground to button. It will be simply stretched to size of button. I don't think quality will become bader. – SentineL Nov 22 '11 at 07:49
0

You can use +[UIColor colorWithPatternImage] in iOS:

NSString *path = 
    [[NSBundle mainBundle] pathForResource:@"<# the pattern file #>" ofType:@"png"];
UIColor *patternColor = [UIColor colorWithPatternImage:
    [UIImage imageWithContentsOfFile:path]];
/* use patternColor anywhere as a regular UIColor instance */

It works better with seamless patterns. For OSX you can use +[NSColor colorWithPatternImage] method.

djromero
  • 19,551
  • 4
  • 71
  • 68