2011-03-04 4 views
3

iphone 애플 리케이션을 개발 오전 나는 그것을 이미지 테두리를 줄 수 있도록 런타임에 이미지를 조작해야합니다.어떻게 두 이미지 UIImage 만들 수 있습니다 : 그림 및 테두리

테두리 나 배경 이미지로 두 개의 UIImages를 결합해야하며, 다른 UIImage는 첫 번째 내부에 상주 할 것입니다. 그래서 가장 좋은 방법은 무엇입니까 ??

UIGraphicsGetCurrentContext()와 같은 일부 기능에 대해 읽었지만 주 스레드에서 수행해야하며 여러 번 호출해야하는 경량 코드가 필요합니다.

감사

답변

1

테두리에 투명 경우, 당신은 그냥 당신의 이미지와 UIImageView을 만들고, 거기에 당신의 테두리가 다른 UIImageView 다음 상단에있는 테두리, 뷰에 파단으로 추가 할 수 이미지의

1

두 번째 UIImageView를 첫 번째 하위 뷰 (Border imageView)의 하위보기로 만들면 어떨까요?

+0

+1. 예 ... 아주 간단합니다. –

1

이것은 내가 돌아 왔을 때 발견 한 코드 중 하나이며 내 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; 
} 
  • 내가 합병증없이 백그라운드 스레드에서 사용합니다.
7

이미지 처리

여기에 (, 당신에게 아이디어를 보여주기 위해 테스트하지) 예입니다 :

-(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로 행을 교환하십시오). 그냥 디스플레이

아니면 만 표시 목적으로이 원하는 경우, 당신이보기를 오버레이 할 수 또는 석영을 사용하고있는 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; 
관련 문제