Я использую следующий код для создания растрового изображения.Черная рамка в растровом изображении в iOS
UIGraphicsBeginImageContextWithOptions(CGSizeMake(targetWidth, targetHeight), NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context,CGRectMake(0, 0, targetWidth, targetHeight));
float red ,green, blue ,alpha ;
for (int Row = 1; Row <= targetHeight; Row++)
{
if (Row <= originalHeight) {
for (int Col = 0; Col < targetWidth; iCol++)
{
if (Col < originalWidth) {
UIColor *color = [originalImage colorAtPixel:CGPointMake(Col, Row) :originalImage];
[color getRed:&red green:&green blue:&blue alpha:&alpha];
if (red == 0.0 && green == 0.0 && blue == 0.0 && alpha == 0.0) {
CGContextSetRGBFillColor(context, 0 , 0, 0 , 0); //set transparent pixels
}
else {
CGContextSetRGBFillColor(context, red , green, blue , 1);
}
CGContextFillRect(context, CGRectMake(Col,Row,1,1));
}
}
}
}
finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//below code is used from another link
- (UIColor *)colorAtPixel:(CGPoint)point :(UIImage*)image {
// Cancel if point is outside image coordinates
if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, image.size.width, image.size.height), point)) {
return nil;
}
NSInteger pointX = trunc(point.x);
NSInteger pointY = trunc(point.y);
CGImageRef cgImage = image.CGImage;
NSUInteger width = image.size.width;
NSUInteger height = image.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
int bytesPerPixel = 4;
int bytesPerRow = bytesPerPixel * 1;
NSUInteger bitsPerComponent = 8;
unsigned char pixelData[4] = { 0, 0, 0, 0 };
CGContextRef context = CGBitmapContextCreate(pixelData,
1,
1,
bitsPerComponent,
bytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextSetBlendMode(context, kCGBlendModeCopy);
// Draw the pixel we are interested in onto the bitmap context
CGContextTranslateCTM(context, -pointX, pointY-(CGFloat)height);
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage);
CGContextRelease(context);
// Convert color values [0..255] to floats [0.0..1.0]
CGFloat red = (CGFloat)pixelData[0]/255.0f;
CGFloat green = (CGFloat)pixelData[1]/255.0f;
CGFloat blue = (CGFloat)pixelData[2]/255.0f;
CGFloat alpha = (CGFloat)pixelData[3]/255.0f;
return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}
Я хочу, чтобы изображение выглядело прозрачным. Я изменяю черный фон контекста на прозрачный, используя CGContextSetRGBFillColor (контекст, 0, 0, 0, 0); // set transparent
который отлично работает. Но там он все еще оставляет черную рамку на изображении. Я хочу ее удалить. Как это можно достичь? Любые указатели?
Что произойдет, если вы измените случай else на 'CGContextSetRGBFillColor (контекст, красный, зеленый, синий, альфа),'? –
может быть, это поможет вам или кому-то еще. Существует классное приложение под названием Paintcode. Хотя это платное приложение, вы можете использовать демоверсию, чтобы увидеть сгенерированный код изображения, и, возможно, получить некоторые новые идеи о написании (как это случилось со мной) – user1244109
@CleverError. Ты прав. Я пропустил эту часть. Я использовал альфа вместо 1. Теперь он отлично работает. Спасибо всем за помощь! – XYZ