相关疑难解决方法(0)

通过向现有UIImage添加阴影来创建新的UIImage

我看了一下这个问题:UIImage Shadow Trouble

但接受的答案对我不起作用.

我正在尝试做的是采用UIImage并为其添加阴影,然后返回一个全新的UIImage,阴影和所有.

这就是我正在尝试的:

- (UIImage*)imageWithShadow {

    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef shadowContext = CGBitmapContextCreate(NULL, self.size.width, self.size.height + 1, CGImageGetBitsPerComponent(self.CGImage), 0, 
                                                 colourSpace, kCGImageAlphaPremultipliedLast);
    CGColorSpaceRelease(colourSpace);

    CGContextSetShadow(shadowContext, CGSizeMake(0, -1), 1);
    CGContextDrawImage(shadowContext, CGRectMake(0, 0, self.size.width, self.size.height), self.CGImage);

    CGImageRef shadowedCGImage = CGBitmapContextCreateImage(shadowContext);
    CGContextRelease(shadowContext);

    UIImage * shadowedImage = [UIImage imageWithCGImage:shadowedCGImage];
    CGImageRelease(shadowedCGImage);

    return shadowedImage;
}
Run Code Online (Sandbox Code Playgroud)

结果是我得到了与之前通过此方法完全相同的图像.

我这样做是正确的,或者有什么明显的东西我不见了?

iphone cocoa-touch objective-c uiimage cgcontext

10
推荐指数
5
解决办法
1万
查看次数

如何在IOS中创建Box-Shadow

我想设计一个像这样的登录页面 在此输入图像描述

在采用箱阴影在IOS当前页我试图层阴影像这样 但是这一个未示出像上述图像PLZ指南.

我试过了

imageView.layer.shadowColor = [UIColor purpleColor].CGColor;
imageView.layer.shadowOffset = CGSizeMake(0, 1);
imageView.layer.shadowOpacity = 1;
imageView.layer.shadowRadius = 1.0;
imageView.clipsToBounds = NO;
Run Code Online (Sandbox Code Playgroud)

但它表明

在此输入图像描述

提前致谢

更新

使用holex代码:

UIImageView *imag = (UIImageView*)[self.view viewWithTag:1];
    imag = [[UIImageView alloc] initWithFrame:CGRectMake((screenWidth/100)*12, (screenHeight/100)*10, (screenWidth/100)*75, (screenHeight/100)*61)];
    [imag setBackgroundColor:[UIColor whiteColor]];
    [imag.layer setOpacity:0.4];
    [imag.layer setShadowOpacity:1.0];
    [imag.layer setShadowColor:[[UIColor blackColor] CGColor]];
    [imag.layer setShadowOffset:CGSizeMake(0.0, 0.0)];
    [imag.layer setShadowRadius:8.0];
    [imag setClipsToBounds:FALSE];
    [imag.layer setMasksToBounds:FALSE];
    [self.view addSubview:imag];
Run Code Online (Sandbox Code Playgroud)

ios 6它显示如下图像,但它在ios 7上工作

在此输入图像描述

objective-c ios

2
推荐指数
1
解决办法
2510
查看次数

UIImage暗影麻烦

我正在尝试为图像添加一个小阴影,就像App Store中的图标阴影一样.现在我正在使用以下代码来围绕我的图像的角落.有谁知道我怎么能适应它来添加一个小阴影?

- (UIImage *)roundCornersOfImage:(UIImage *)source height:(int)height width:(int)width  {
int w = width;
int h = height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef imageContext = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);

CGContextBeginPath(imageContext);

CGRect rect = CGRectMake(0, 0, w, h);
addRoundedRectToPath(imageContext, rect, 10, 10);
CGContextClosePath(imageContext);
CGContextClip(imageContext);

CGContextDrawImage(imageContext, CGRectMake(0, 0, w, h), source.CGImage);

CGImageRef imageMasked = CGBitmapContextCreateImage(imageContext);
CGContextRelease(imageContext);
CGColorSpaceRelease(colorSpace);

return [UIImage imageWithCGImage:imageMasked];    
}
Run Code Online (Sandbox Code Playgroud)

"addRoundedRectToPath"指的是另一种明显围绕角落的方法.

iphone uiimage

1
推荐指数
1
解决办法
9450
查看次数

标签 统计

iphone ×2

objective-c ×2

uiimage ×2

cgcontext ×1

cocoa-touch ×1

ios ×1