0

NSbundle に画像があります。以下は画像です

http://tinypic.com/view.php?pic=2093tvt&s=5

この画像をマスク画像として使用し、任意の画像に適用して、他の画像が上記の画像の形状と同じ形状になるようにします。

そのため、重複した質問ですが、私にとってはうまくいきません..

上記の質問のように、画像をグラスケールすることが提案されたので、同じことをしました。コードで画像をグレースケールに変更しました

- (UIImage *) convertToGreyscale:(UIImage *)i {

int kRed = 1;
int kGreen = 2;
int kBlue = 4;

int colors = kGreen;
int m_width = i.size.width;
int m_height = i.size.height;

uint32_t *rgbImage = (uint32_t *) malloc(m_width * m_height * sizeof(uint32_t));
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(rgbImage, m_width, m_height, 8, m_width * 4, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextSetShouldAntialias(context, NO);
CGContextDrawImage(context, CGRectMake(0, 0, m_width, m_height), [i CGImage]);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);

// now convert to grayscale
uint8_t *m_imageData = (uint8_t *) malloc(m_width * m_height);
for(int y = 0; y < m_height; y++) {
    for(int x = 0; x < m_width; x++) {
        uint32_t rgbPixel=rgbImage[y*m_width+x];
        uint32_t sum=0,count=0;
        if (colors & kRed) {sum += (rgbPixel>>24)&255; count++;}
        if (colors & kGreen) {sum += (rgbPixel>>16)&255; count++;}
        if (colors & kBlue) {sum += (rgbPixel>>8)&255; count++;}
        m_imageData[y*m_width+x]=sum/count;
    }
}
free(rgbImage);

// convert from a gray scale image back into a UIImage
uint8_t *result = (uint8_t *) calloc(m_width * m_height *sizeof(uint32_t), 1);

// process the image back to rgb
for(int i = 0; i < m_height * m_width; i++) {
    result[i*4]=0;
    int val=m_imageData[i];
    result[i*4+1]=val;
    result[i*4+2]=val;
    result[i*4+3]=val;
}

// create a UIImage
colorSpace = CGColorSpaceCreateDeviceRGB();
context = CGBitmapContextCreate(result, m_width, m_height, 8, m_width * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
CGImageRef image = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
UIImage *resultUIImage = [UIImage imageWithCGImage:image];
CGImageRelease(image);

free(m_imageData);

// make sure the data will be released by giving it to an autoreleased NSData
[NSData dataWithBytesNoCopy:result length:m_width * m_height];

return resultUIImage;
}

以下はグレースケール画像です

リンク1:

そして、画像をマスクすると、結果として、コメントで指定された結果リンクの下になります。2 つ以上のリンクを追加できません

リンク2:

適切なマスク画像を作成していないと思います

画像の下にマスクしようとしています http://tinypic.com/view.php?pic=10di24m&s=5

マスク画像の作り方を教えてください。

4

1 に答える 1

0

あなたが使用しているメソッド graysclae に問題があるようです。円の外側の色を黒にしますが、適切にマスクするには白にする必要があります。

ここに私が書いた画像カテゴリがあります

UIImage+Mask.h

#import <UIKit/UIKit.h>

@interface UIImage (Mask)

- (UIImage*) maskImage:(UIImage *) image;
- (UIImage *)greyscaleImage;
@end

UIImage+Mask.m

@implementation UIImage (Mask)


- (UIImage*) maskImage:(UIImage *) image
{
    CGImageRef imageReference = image.CGImage;
    CGImageRef maskReference = self.CGImage;

    CGImageRef imageMask = CGImageMaskCreate(CGImageGetWidth(maskReference),
                                             CGImageGetHeight(maskReference),
                                             CGImageGetBitsPerComponent(maskReference),
                                             CGImageGetBitsPerPixel(maskReference),
                                             CGImageGetBytesPerRow(maskReference),
                                             CGImageGetDataProvider(maskReference),
                                             NULL, // Decode is null
                                             YES // Should interpolate
                                             );

    CGImageRef maskedReference = CGImageCreateWithMask(imageReference, imageMask);
    CGImageRelease(imageMask);

    UIImage *maskedImage = [UIImage imageWithCGImage:maskedReference];
    CGImageRelease(maskedReference);

    return maskedImage;
}

- (UIImage *)greyscaleImage {

    CGImageRef imgRef = self.CGImage;

    // Get the image width
    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);

    // Create a Grayscale colour space
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();

    /*
     * Create the context - based on orientation
     */
    CGContextRef context = nil;

    // If the orientation isn't UIImageOrientationUp then we'll need to rotate
    UIImageOrientation orient = self.imageOrientation;

    switch (orient) {
        case UIImageOrientationUp:
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
        case UIImageOrientationUpMirrored:

            context = CGBitmapContextCreate(NULL, width, height, 8, 0, colorSpace, kCGImageAlphaNone);
            break;

        default:
            context = CGBitmapContextCreate(NULL, height, width, 8, 0, colorSpace, kCGImageAlphaNone);
            break;
    }

    CGColorSpaceRelease(colorSpace);

    /*
     * Create a context translation/rotation based on the orientation
     */
    switch(orient) {
        case UIImageOrientationUpMirrored:
            // 2 = 0th row is at the top, and 0th column is on the right - Flip Horizontal
            CGContextConcatCTM(context, CGAffineTransformMake(-1.0, 0.0, 0.0, 1.0, width, 0.0));
            break;

        case UIImageOrientationDown:
            // 3 = 0th row is at the bottom, and 0th column is on the right - Rotate 180 degrees
            CGContextConcatCTM(context, CGAffineTransformMake(-1.0, 0.0, 0.0, -1.0, width, height));
            break;

        case UIImageOrientationDownMirrored:
            // 4 = 0th row is at the bottom, and 0th column is on the left - Flip Vertical
            CGContextConcatCTM(context, CGAffineTransformMake(1.0, 0.0, 0, -1.0, 0.0, height));
            break;

        case UIImageOrientationLeftMirrored:
            // 5 = 0th row is on the left, and 0th column is the top - Rotate -90 degrees and Flip Vertical
            CGContextConcatCTM(context, CGAffineTransformMake(0.0, -1.0, -1.0, 0.0, height, width));
            break;

        case UIImageOrientationRight:
            // 6 = 0th row is on the right, and 0th column is the top - Rotate 90 degrees
            CGContextConcatCTM(context, CGAffineTransformMake(0.0, -1.0, 1.0, 0.0, 0.0, width));
            break;

        case UIImageOrientationRightMirrored:
            // 7 = 0th row is on the right, and 0th column is the bottom - Rotate 90 degrees and Flip Vertical
            CGContextConcatCTM(context, CGAffineTransformMake(0.0, 1.0, 1.0, 0.0, 0.0, 0.0));
            break;

        case UIImageOrientationLeft:
            // 8 = 0th row is on the left, and 0th column is the bottom - Rotate -90 degrees
            CGContextConcatCTM(context, CGAffineTransformMake(0.0, 1.0, -1.0, 0.0, height, 0.0));
            break;

        case UIImageOrientationUp:
            // Nothing to do here
            break;
    }

    /*
     * Create the new image
     */
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextFillRect(context, CGRectMake(0, 0, width, height));
    //CGContextFillEllipseInRect(context, );
    // Draw the image to the context
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imgRef);

    // Get a UIImage from the context
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
    UIImage *imageCopy =  [UIImage imageWithCGImage:imageRef];

    // Release
    CGImageRelease(imageRef);
    CGContextRelease(context);

    return imageCopy;
}

@end

使い方はこちら

UIImage *maskImage = [UIImage imageNamed:@"maskimage"];  // in your case the red dot image
UIImage *image = [UIImage imageNamed:@"image"];   // image to mask
maskImage = [maskImage greyscaleImage];
image = [maskImage maskImage:image];
self.imageView.image = image;
于 2013-10-15T11:01:52.920 に答える