简体   繁体   English

如何从两个图像创建UIImage:图片和边框

[英]how can i create an UIImage from two images : picture and border

i'm developing an iphone app , i need to manipulate a image at run time so that i can give it an image border . 我正在开发一个iPhone应用程序,我需要在运行时操作图像,以便我可以给它一个图像边框。

i need to combine two UIImages , one as a border or background image , and the other UIImage will reside inside the first , so what's the best way to do it ?? 我需要结合两个UIImages,一个作为边框或背景图像,另一个UIImage将驻留在第一个,所以最好的方法是什么?

i read about some functions like , UIGraphicsGetCurrentContext() but it seems that it should be done at main thread and i need some lighter code cause i need to call it several times . 我读了一些函数,比如UIGraphicsGetCurrentContext(),但它似乎应该在主线程完成,我需要一些较轻的代码,因为我需要多次调用它。

thanks 谢谢

Image Manipulation 图像处理

Here's 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;
}

It draws background image, than your image, which will be quite smaller. 它绘制背景图像,而不是图像,这将是非常小的。 You can use this if you do want to really manipulate with image and your borderImage is not transparent. 如果你想真正使用图像操作并且borderImage不透明,你可以使用它。 If it is transparent, you can draw it over your image (swap lines with drawRect: calls). 如果它是透明的,您可以在图像上绘制它(使用drawRect:calls交换行)。

Just for Display 仅供展示

Or if you want this for display purpose only, you can overlay views or you can use Quartz and set some sort of border via CALayer. 或者,如果您只想将其用于显示目的,则可以叠加视图,也可以使用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;

如果您的边框是透明的,您可以创建一个包含图像的UIImageView ,另一个包含边框的UIImageView ,然后将它们作为子视图添加到视图中,边框位于图像顶部。

你为什么不把第二个UIImageView作为第一个子视图(Border imageView)?

This is a piece of code I found way back when and use in my UIView extension. 这是我在UIView扩展中使用时找到的一段代码。 Not sure who to credit for it, but here it 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 on a background thread without complications. 我在后台线程上使用它而没有并发症。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM