繁体   English   中英

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

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

我正在开发一个iPhone应用程序,我需要在运行时操作图像,以便我可以给它一个图像边框。

我需要结合两个UIImages,一个作为边框或背景图像,另一个UIImage将驻留在第一个,所以最好的方法是什么?

我读了一些函数,比如UIGraphicsGetCurrentContext(),但它似乎应该在主线程完成,我需要一些较轻的代码,因为我需要多次调用它。

谢谢

图像处理

这是一个例子(未经过测试,只是为了向您展示一个想法):

-(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;

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

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

这是我在UIView扩展中使用时找到的一段代码。 不知道该归功于谁,但这里是:

- (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;
}
  • 我在后台线程上使用它而没有并发症。

暂无
暂无

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

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