How can I create a UIImage from two images: image and frame

I am developing an application for iphone, I need to manipulate the image at runtime so that I can provide it with an image border.

I need to combine two UIImages, one as a border or background image, and the other UIImage will be inside the first, so what's the best way to do this?

I read about some functions like UIGraphicsGetCurrentContext (), but it seems that this should be done in the main thread, and I need some lighter code, because I need to call it several times.

thank

+3
source share
4 answers

Image manipulation

Here is an example (not tested, just to show you an idea):

-(UIImage *)imageWithImage:(UIImage *)image borderImage:(UIImage *)borderImage covertToSize:(CGSize)size {
    UIGraphicsBeginImageContext(size);
    [borderImage drawInRect:CGRectMake( 0, 0, size.width, size.height )];
    [image drawInRect:CGRectMake( 10, 10, size.width - 20, size.height - 20)];
    UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return destImage;
}

, , . , , borderImage . , ( drawRect: calls).

, , , Quartz CALayer.

self.imageView.layer.cornerRadius = 3.0;
self.imageView.layer.masksToBounds = YES;
self.imageView.layer.borderColor = [UIColor blackColor].CGColor;
self.imageView.layer.borderWidth = 1.0;
+7

, UIImageView UIImageView , , .

+1

UIImageView (Border imageView)?

+1

This is a piece of code that I found back that I use in my UIView extension. Not sure who lends for him, but here he is:

- (UIImage *)overlayWithImage:(UIImage *)image2 {

    UIImage *image1 = self;
    CGRect drawRect = CGRectMake(0.0, 0.0, image1.size.width, image1.size.height);

    // Create the bitmap context
    CGContextRef    bitmapContext = NULL;
    void *          bitmapData;
    int             bitmapByteCount;
    int             bitmapBytesPerRow;
    CGSize          size = CGSizeMake(image1.size.width, image1.size.height);

    // Declare the number of bytes per row. Each pixel in the bitmap in this
    // example is represented by 4 bytes; 8 bits each of red, green, blue, and
    // alpha.

        bitmapBytesPerRow   = (size.width * 4);
        bitmapByteCount     = (bitmapBytesPerRow * size.height);

    // Allocate memory for image data. This is the destination in memory
    // where any drawing to the bitmap context will be rendered.

        bitmapData = malloc( bitmapByteCount );

        if (bitmapData == NULL) {

            return nil;
        }

// Create the bitmap context. We want pre-multiplied ARGB, 8-bits
// per component. Regardless of what the source image format is
// (CMYK, Grayscale, and so on) it will be converted over to the format
// specified here by CGBitmapContextCreate.

        bitmapContext = CGBitmapContextCreate (bitmapData, size.width, size.height,8,bitmapBytesPerRow,                 CGColorSpaceCreateDeviceRGB(),kCGImageAlphaNoneSkipFirst);

        if (bitmapContext == NULL)

// error creating context

             return nil;

// Draw the image to the bitmap context. Once we draw, the memory
// allocated for the context for rendering will then contain the
// raw image data in the specified color space.

        CGContextDrawImage(bitmapContext, drawRect, [image1 CGImage]);
    CGContextDrawImage(bitmapContext, drawRect, [image2 CGImage]);
    CGImageRef   img = CGBitmapContextCreateImage(bitmapContext);
    UIImage*     ui_img = [UIImage imageWithCGImage: img];

    CGImageRelease(img);
    CGContextRelease(bitmapContext);
    free(bitmapData);

    return ui_img;
}
  • I use this in the background thread without complications.
+1
source

Source: https://habr.com/ru/post/1796282/


All Articles