2

放射状グラデーション (ポイント > 円) を新しい UIImage (iphone) にレンダリングする方法を考えてみてください。私は次を見ました:

http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_shadings/dq_shadings.html

そして、CGShadingRefまたはCGGradientRefを使用し、UIImageの「imageWithCGImage」コンストラクターを使用してCG *からUIImageに移動する必要があると思いました...しかし、方法がわかりません。

どんな提案でも大歓迎です!

4

5 に答える 5

10

これが実用的なソリューションの要点です。何か見逃した場合はお知らせください(ハンドル/参照の解放など)

私のブログにも投稿されています: http://splinter.com.au/rendering-a-radial-gradient-on-the-iphone-obj

- (UIImage *)radialGradientImage:(CGSize)size start:(float)start end:(float)end centre:(CGPoint)centre radius:(float)radius {
    // Render a radial background
    // http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_shadings/dq_shadings.html

    // Initialise
    UIGraphicsBeginImageContextWithOptions(size, YES, 1);

    // Create the gradient's colours
    size_t num_locations = 2;
    CGFloat locations[2] = { 0.0, 1.0 };
    CGFloat components[8] = { start,start,start, 1.0,  // Start color
        end,end,end, 1.0 }; // End color

    CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB();
    CGGradientRef myGradient = CGGradientCreateWithColorComponents (myColorspace, components, locations, num_locations);

    // Normalise the 0-1 ranged inputs to the width of the image
    CGPoint myCentrePoint = CGPointMake(centre.x * size.width, centre.y * size.height);
    float myRadius = MIN(size.width, size.height) * radius;

    // Draw it!
    CGContextDrawRadialGradient (UIGraphicsGetCurrentContext(), myGradient, myCentrePoint,
                                 0, myCentrePoint, myRadius,
                                 kCGGradientDrawsAfterEndLocation);

    // Grab it as an autoreleased image
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    // Clean up
    CGColorSpaceRelease(myColorspace); // Necessary?
    CGGradientRelease(myGradient); // Necessary?
    UIGraphicsEndImageContext(); // Clean up
    return image;
}
于 2011-04-11T23:06:42.643 に答える
2

また、iOS5+ で CoreImage を使用し、Vignette Filter を使用することもできます。

- (UIImage *)vignetteImageOfSize:(CGSize)size withImage:(UIImage *)image {  
    UIGraphicsBeginImageContextWithOptions(size, YES, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextFillRect(context, CGRectMake(0.0, 0.0, size.width, size.height));

    CIImage *coreImage = [CIImage imageWithCGImage:image.CGImage];
    CGPoint origin = [coreImage extent].origin;
    CGAffineTransform translation =
    CGAffineTransformMakeTranslation(-origin.x, -origin.y);
    coreImage = [coreImage imageByApplyingTransform:translation];

    CIFilter *vignette = [[CIFilter filterWithName:@"CIVignette"] retain];
    [vignette setValue:@1.5 forKey:@"inputRadius"];
    [vignette setValue:@1.5 forKey:@"inputIntensity"];
    [vignette setValue:coreImage forKey:@"inputImage"];

    UIImage *vignetteImage = [UIImage imageWithCIImage:vignette.outputImage];
    [vignette release];

    CGRect imageFrame = CGRectMake(0.0, 0.0, size.width, size.height);
    [image drawInRect:imageFrame];
    UIImage *renderedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return renderedImage;
}
于 2012-09-11T20:47:52.700 に答える
1

この単純化されたメソッドをここに書きました (たとえば、UIImage カテゴリに入れます)。

+ (UIImage *)radialGradientImageWithRadius:(CGFloat)radius StartColor:(UIColor*)startColor EndColor:(UIColor*)endColor ApplyScreenScale:(BOOL)useScreenScale
{
    // Initialize
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(radius * 2, radius * 2), NO, (useScreenScale ? 0.f : 1.f));

    CGContextRef context = UIGraphicsGetCurrentContext();

    // bottom glow gradient
    CGColorSpaceRef colourspace = CGColorSpaceCreateDeviceRGB();

    // build color components
    CGFloat red1 = 0.f, green1 = 0.f, blue1 = 0.f, alpha1 = 0.f;
    [(startColor == nil ? [UIColor clearColor] : startColor) getRed:&red1 green:&green1 blue:&blue1 alpha:&alpha1];
    CGFloat red2 = 0.f, green2 = 0.f, blue2 = 0.f, alpha2 = 0.f;
    [(endColor == nil ? [UIColor clearColor] : endColor) getRed:&red2 green:&green2 blue:&blue2 alpha:&alpha2];

    CGFloat cComponents[] = { red1, green1, blue1, alpha1, red2, green2, blue2, alpha2 };
    CGFloat cGlocations[] = { 0.f, 1.f };

    CGGradientRef gradient = CGGradientCreateWithColorComponents(colourspace, cComponents, cGlocations, 2);
    CGPoint centerPoint = CGPointMake(radius, radius);

    CGContextDrawRadialGradient(context, gradient, centerPoint, 0.f, centerPoint, radius , 0.f);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    CGGradientRelease(gradient);
    CGColorSpaceRelease(colourspace);
    UIGraphicsEndImageContext();

    return image;
}

使用例:

// resulting image size 128x128 px
UIImage* myRadialImage = [UIImage radialGradientImageWithRadius:128.f StartColor:[UIColor greenColor] EndColor:nil ApplyScreenScale:NO]; 

// resulting image size 256x256 px on normal retina display, or 384x384 on iPhone 6 or gre 
UIImage* myRadialImage = [UIImage radialGradientImageWithRadius:128.f StartColor:[UIColor greenColor] EndColor:[UIColor redColor] ApplyScreenScale:YES]; 

役に立つことを願っています

于 2015-09-03T09:07:34.677 に答える
1

リンクしたセクションと同じドキュメントでGraphics Contextsについて読む必要があります。すべての描画は、グラフィック コンテキストで行われます。放射状グラデーション、線形グラデーション、またはその他のものを含む画像を作成する場合は、次のことを行う必要があります。

于 2011-04-11T09:08:54.783 に答える