如何设置一个uiimage alpha的不透明度/alpha

收藏,1.2k 浏览
问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
_coverLabel = [UILabel new];
_coverLabel.alpha = 0.8;
_coverLabel.textColor = [UIColor whiteColor];
_coverLabel.text = @"设置封面";
设置UILabel的alpha属性后文字的透明度也跟着变了。
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
5月18日 回答
刚刚测试了以下,可以设置label的backgroundcolor的透明度,就不会影响label字体的颜色了。
同步到新浪微博
分享到微博?
与我们一起探索更多的未知
专业的开发者技术社区,为用户提供多样化的线上知识交流,丰富的线下活动及给力的工作机会
加入只需一步
关闭理由:
删除理由:
忽略理由:
推广(招聘、广告、SEO 等)方面的内容
与已有问题重复(请编辑该提问指向已有相同问题)
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
无法获得确切结果的问题
非开发直接相关的问题
非技术提问的讨论型问题
其他原因(请补充说明)
我要举报该,理由是:
扫扫下载 App
SegmentFault
一起探索更多未知主题 : iphone 开发 。我想修改一张图片的指定区域的透明度
级别: 新手上路
可可豆: 62 CB
威望: 62 点
在线时间: 159(时)
发自: Web Page
iphone 开发 。我想修改一张图片的指定区域的透明度&&&
本帖被 angellixf 从 OS X开发讨论区 移动到本区()
技术问题发到问答:我知道,我仍旧要在论坛继续发布问题
rt比如说我有一张图片,然后希望修改这张图片的中心位置的透明度,其余地方不变。 希望有大神帮我解答一下,或者提供一下思路。类似这样的一个效果,之前我的想法是用类似画图的橡皮擦类似的方法,手指划过的地方就变为透明的。但是这样出来的效果非常差,上下两张图之间分隔很明显,没有一个渐变的过程。于是想到了第二个方案,是不是可以手指划过的地方,就修改它的透明度,让透明度逐渐降低,而不是直接透明,但是具体实现的时候就遇见了这个问题,我怎么将我手指划过的位置的alpha值进行修改呢。[ 此帖被lpgonghan在 15:51重新编辑 ]
级别: 圣骑士
UID: 145603
可可豆: 568 CB
威望: 1036 点
在线时间: 374(时)
发自: Web Page
截取指定区域生成一个image,之后用一个同样大小的alpha为0的view覆盖,合成新的image。修改之前截取image的alpha,添加到原先位置,然后合成。
  CGImageRef imageRef = CGBitmapContextCreateImage(context);    self.drawImageObj = [UIImage imageWithCGImage:imageRef];    CGImageRelease(imageRef);    UIImagePNGRepresentation([view currentContextImage])不对,好像错了,之前写过涂鸦,画线的时候两条线相交可以设置混合模式的,比如新的完全覆盖旧的,或者相交的部分按一定的比例混合,我以为和这个差不多。。不知道图片合成行不行。。[ 此帖被lingyj在 15:27重新编辑 ]
级别: 圣骑士
UID: 145603
可可豆: 568 CB
威望: 1036 点
在线时间: 374(时)
发自: Web Page
混合是可以的,那么混合的时候的应该也是可以设置混合模式的吧
级别: 新手上路
可可豆: 62 CB
威望: 62 点
在线时间: 159(时)
发自: Web Page
回 2楼(lingyj) 的帖子
谢谢拉,我先去看看。
关注本帖(如果有新回复会站内信通知您)
论坛技术问题应该发布到? 正确答案:CocoaChina问答
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 浏览移动版iOS UIImage扩展方法(category):放大、旋转、合并UIImage、增加渐变层、添加阴影、调节透明度、保存到相... - 推酷
iOS UIImage扩展方法(category):放大、旋转、合并UIImage、增加渐变层、添加阴影、调节透明度、保存到相...
一有用的 UIImage 扩展,支持(等比例)放大和旋转可在许多 App 中使用。
UIImage-Extensions.h
#import &Foundation/Foundation.h&
#import &UIKit/UIKit.h&
CGFloat DegreesToRadians(CGFloat degrees);
CGFloat RadiansToDegrees(CGFloat radians);
@interface UIImage (JQ_Extensions)
- (UIImage *)imageAtRect:(CGRect)- (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetS
- (UIImage *)imageByScalingToSize:(CGSize)targetS
- (UIImage *)imageRotatedByRadians:(CGFloat)
- (UIImage *)imageRotatedByDegrees:(CGFloat)
- (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2;
[objc] view plaincopyprint?
- (UIImage *) imageWithBackgroundColor:(UIColor *)bgColor
shadeAlpha1:(CGFloat)alpha1
shadeAlpha2:(CGFloat)alpha2
shadeAlpha3:(CGFloat)alpha3
shadowColor:(UIColor *)shadowColor
shadowOffset:(CGSize)shadowOffset
shadowBlur:(CGFloat)shadowB
- (UIImage *)imageWithShadowColor:(UIColor *)shadowColor
shadowOffset:(CGSize)shadowOffset
shadowBlur:(CGFloat)shadowB&br&
- (UIImage *)imageByApplyingAlpha:(CGFloat)
( void ) saveToAlbumWithMetadata: ( NSDictionary
* ) metadata
customAlbumName: ( NSString
* ) customAlbumName
completionBlock: ( void
( ^ )( void )) completionBlock
failureBlock: ( void
( ^ )( NSError
* error )) failureBlock
UIImage-Extensions.m
#import &UIImage-Extensions.h&
CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
CGFloat RadiansToDegrees(CGFloat radians) {return radians * 180/M_PI;};
@implementation UIImage (JQ_Extensions)
-(UIImage *)imageAtRect:(CGRect)rect
CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], rect);
UIImage* subImage = [UIImage imageWithCGImage: imageRef];
CGImageRelease(imageRef);
return subI
- (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize {
UIImage *sourceImage =
UIImage *newImage =
CGSize imageSize = sourceImage.
CGFloat width = imageSize.
CGFloat height = imageSize.
CGFloat targetWidth = targetSize.
CGFloat targetHeight = targetSize.
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetW
CGFloat scaledHeight = targetH
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
CGFloat widthFactor = targetWidth /
CGFloat heightFactor = targetHeight /
if (widthFactor & heightFactor)
scaleFactor = widthF
scaleFactor = heightF
scaledWidth
= width * scaleF
scaledHeight = height * scaleF
// center the image
if (widthFactor & heightFactor) {
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
} else if (widthFactor & heightFactor) {
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
// this is actually the interesting part:
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectZ
thumbnailRect.origin = thumbnailP
thumbnailRect.size.width
thumbnailRect.size.height = scaledH
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if(newImage == nil) NSLog(@&could not scale image&);
return newI
- (UIImage *)imageByScalingToSize:(CGSize)targetSize {
UIImage *sourceImage =
UIImage *newImage =
CGSize imageSize = sourceImage.
CGFloat width = imageSize.
CGFloat height = imageSize.
CGFloat targetWidth = targetSize.
CGFloat targetHeight = targetSize.
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetW
CGFloat scaledHeight = targetH
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
// this is actually the interesting part:
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectZ
thumbnailRect.origin = thumbnailP
thumbnailRect.size.width
thumbnailRect.size.height = scaledH
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if(newImage == nil) NSLog(@&could not scale image&);
return newI
- (UIImage *)imageRotatedByRadians:(CGFloat)radians
return [self imageRotatedByDegrees:RadiansToDegrees(radians)];
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees
// calculate the size of the rotated view's containing box for our drawing space
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
rotatedViewBox.transform =
CGSize rotatedSize = rotatedViewBox.frame.
[rotatedViewBox release];
// Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
// Move the origin to the middle of the image so we will rotate and scale around the center.
CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
// Rotate the image context
CGContextRotateCTM(bitmap, DegreesToRadians(degrees));
// Now, draw the rotated/scaled image into the context
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), [self CGImage]);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newI
// Combine two UIImages
[objc] view plaincopyprint?
- (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {
UIGraphicsBeginImageContext(image2.size);
// Draw image1
[image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];
// Draw image2
[image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();&br&
return resultingI
[objc] view plaincopyprint?
- (UIImage *) imageWithBackgroundColor:(UIColor *)bgColor
shadeAlpha1:(CGFloat)alpha1
shadeAlpha2:(CGFloat)alpha2
shadeAlpha3:(CGFloat)alpha3
shadowColor:(UIColor *)shadowColor
shadowOffset:(CGSize)shadowOffset
shadowBlur:(CGFloat)shadowBlur {
UIImage *image =
CGColorRef cgColor = [bgColor CGColor];
CGColorRef cgShadowColor = [shadowColor CGColor];
CGFloat components[16] = {1,1,1,alpha1,1,1,1,alpha1,1,1,1,alpha2,1,1,1,alpha3};
CGFloat locations[4] = {0,0.5,0.6,1};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef colorGradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, (size_t)4);
CGRect contextR
contextRect.origin.x = 0.0f;
contextRect.origin.y = 0.0f;
contextRect.size = [image size];
//contextRect.size = CGSizeMake([image size].width+5,[image size].height+5);
// Retrieve source image and begin image context
UIImage *itemImage =
CGSize itemImageSize = [itemImage size];
CGPoint itemImageP
itemImagePosition.x = ceilf((contextRect.size.width - itemImageSize.width) / 2);
itemImagePosition.y = ceilf((contextRect.size.height - itemImageSize.height) / 2);
UIGraphicsBeginImageContext(contextRect.size);
CGContextRef c = UIGraphicsGetCurrentContext();
// Setup shadow
CGContextSetShadowWithColor(c, shadowOffset, shadowBlur, cgShadowColor);
// Setup transparency layer and clip to mask
CGContextBeginTransparencyLayer(c, NULL);
CGContextScaleCTM(c, 1.0, -1.0);
CGContextClipToMask(c, CGRectMake(itemImagePosition.x, -itemImagePosition.y, itemImageSize.width, -itemImageSize.height), [itemImage CGImage]);
// Fill and end the transparency layer
CGContextSetFillColorWithColor(c, cgColor);
contextRect.size.height = -contextRect.size.
CGContextFillRect(c, contextRect);
CGContextDrawLinearGradient(c, colorGradient,CGPointZero,CGPointMake(contextRect.size.width*1.0/4.0,contextRect.size.height),0);
CGContextEndTransparencyLayer(c);
//CGPointMake(contextRect.size.width*3.0/4.0, 0)
// Set selected image and end context
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGColorSpaceRelease(colorSpace);
CGGradientRelease(colorGradient);
return resultI
- (UIImage *)imageWithShadowColor:(UIColor *)shadowColor
shadowOffset:(CGSize)shadowOffset
shadowBlur:(CGFloat)shadowBlur
CGColorRef cgShadowColor = [shadowColor CGColor];
CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef shadowContext = CGBitmapContextCreate(NULL, self.size.width, self.size.height,
CGImageGetBitsPerComponent(self.CGImage), 0,
colourSpace, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colourSpace);
// Setup shadow
CGContextSetShadowWithColor(shadowContext, shadowOffset, shadowBlur, cgShadowColor);
CGRect drawRect = CGRectMake(-shadowBlur, -shadowBlur, self.size.width + shadowBlur, self.size.height + shadowBlur);
CGContextDrawImage(shadowContext, drawRect, self.CGImage);
CGImageRef shadowedCGImage = CGBitmapContextCreateImage(shadowContext);
CGContextRelease(shadowContext);
UIImage * shadowedImage = [UIImage imageWithCGImage:shadowedCGImage];
CGImageRelease(shadowedCGImage);
return shadowedI
- (UIImage *)imageByApplyingAlpha:(CGFloat)alpha {
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect area = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -area.size.height);
CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
CGContextSetAlpha(ctx, alpha);
CGContextDrawImage(ctx, area, self.CGImage);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newI
- ( void ) saveToAlbumWithMetadata: ( NSDictionary * ) metadata
customAlbumName: ( NSString * ) customAlbumName
completionBlock: ( void ( ^ )( void )) completionBlock
failureBlock: ( void ( ^ )( NSError * error )) failureBlock
NSData * imageData = UIImagePNGRepresentation ( self );
ALAssetsLibrary * assetsLibrary = [[ ALAssetsLibrary alloc ] init ];
void ( ^ AddAsset )( ALAssetsLibrary * , NSURL * ) = ^ ( ALAssetsLibrary * assetsLibrary , NSURL * assetURL ) {
[ assetsLibrary assetForURL : assetURL resultBlock :^ ( ALAsset * asset ) {
[ assetsLibrary enumerateGroupsWithTypes : ALAssetsGroupAll usingBlock :^ ( ALAssetsGroup * group , BOOL * stop ) {
if ([[ group valueForProperty : ALAssetsGroupPropertyName ] isEqualToString : customAlbumName ]) {
[ group addAsset : asset ];
if ( completionBlock ) {
completionBlock ();
} failureBlock :^ ( NSError * error ) {
if ( failureBlock ) {
failureBlock ( error );
} failureBlock :^ ( NSError * error ) {
if ( failureBlock ) {
failureBlock ( error );
[ assetsLibrary writeImageDataToSavedPhotosAlbum : imageData metadata : metadata completionBlock :^ ( NSURL * assetURL , NSError * error ) {
if ( customAlbumName ) {
[ assetsLibrary addAssetsGroupAlbumWithName : customAlbumName resultBlock :^ ( ALAssetsGroup * group ) {
if ( group ) {
[ assetsLibrary assetForURL : assetURL resultBlock :^ ( ALAsset * asset ) {
[ group addAsset : asset ];
if ( completionBlock ) {
completionBlock ();
} failureBlock :^ ( NSError * error ) {
if ( failureBlock ) {
failureBlock ( error );
AddAsset ( assetsLibrary , assetURL );
} failureBlock :^ ( NSError * error ) {
AddAsset ( assetsLibrary , assetURL );
if ( completionBlock ) {
completionBlock ();
已发表评论数()
已收藏到推刊!
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
排版有问题
没有分页内容
视频无法显示
图片无法显示加入黑色的叠加与0.3的不透明度比的UIImageView-iphone,objective-c,ios,ipad,calayer-CodeGo.net
加入黑色的叠加与0.3的不透明度比的UIImageView
我有一个UIImageView,我想添加一个黑色覆盖在它上面。什么是这样做,而不必重写的drawRect的最好方法是什么?我想在它上面添加的CALayer的。但不知道该如何得到一个黑色的CALayer与.3alpha。
本文地址 :CodeGo.net/473641/
-------------------------------------------------------------------------------------------------------------------------
1. 像这样?
UIView *overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, myImageView.frame.size.width, myImageView.frame.size.height / 2)];
[overlay setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3]];
[myImageView addSubview:overlay];
对MDT的答案是正确的。这只是另一种方式CAGradientLayer旁UIView。我认为它会令你想要什么更多的图形选项。
首先你应该添加
#import &QuartzCore/QuartzCore.h&
你的ViewController.m并且要将此添加叠加层到任何地方你UIImage使用方法:
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = myImageView.layer.
gradientLayer.colors = [NSArray arrayWithObjects:
(id)[UIColor colorWithWhite:0.9f alpha:0.7f].CGColor,
(id)[UIColor colorWithWhite:0.0f alpha:0.3f].CGColor,
gradientLayer.locations = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:0.5f],
//If you want to have a border for this layer also
gradientLayer.borderColor = [UIColor colorWithWhite:1.0f alpha:1.0f].CGC
gradientLayer.borderWidth = 1;
[myImageView.layer addSublayer:gradientLayer];
我希望这将有助于你把它
你有没有考虑增加一个UIView搭配黑色的backgroundColor和0.3alpha作为一个子视图到图像视图?
这类似于MDT的回答CALayer的属性:
UIView *blackOverlay = [[UIView alloc] initWithFrame: imageView.frame];
blackOverlay.layer.backgroundColor = [[UIColor blackColor] CGColor];
blackOverlay.layer.opacity = 0.3f;
[self.view addSubview: blackOverlay];
本文标题 :加入黑色的叠加与0.3的不透明度比的UIImageView
本文地址 :CodeGo.net/473641/
Copyright (C) 2014 CodeGo.net 沪ICP备号 联&系& c&o&d&e&g&o &@&1&2&6&.&c&o&m如何设置一个UIImage的不透明度/alpha_百度知道
如何设置一个UIImage的不透明度/alpha
提问者采纳
0:- (UIImage *)imageByApplyingA CGRect area = CGRectMake(0, NO; CGContextRef ctx = UIGraphicsGetCurrentContext(), self, 0.size:(CGFloat) alpha { UIGraphicsBeginImageContextWithOptions(self1创建UIImage的类别.size.0f)
来自团队:
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 alpha透明度 的文章

 

随机推荐