我有png文件的路径(图片)。我想在同一个文件夹中创建新图像,此图像的中心将是尺寸为300x100的黑色矩形。然后我需要获得新创建的Image的路径。
有人可以帮我解决这个问题吗?
- (void) grayscale:(UIImage*) image {
CGContextRef ctx;
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
// Now your rawData contains the image data in the RGBA8888 pixel format.
NSUInteger byteIndex = (bytesPerRow * 0) + 0 * bytesPerPixel;
for (int ii = 0 ; ii < width * height ; ++ii)
{
// Get color values to construct a UIColor
CGFloat red = (rawData[byteIndex] * 1.0) / 255.0;
CGFloat green = (rawData[byteIndex + 1] * 1.0) / 255.0;
CGFloat blue = (rawData[byteIndex + 2] * 1.0) / 255.0;
CGFloat alpha = (rawData[byteIndex + 3] * 1.0) / 255.0;
rawData[byteIndex] = (char) (red);
rawData[byteIndex+1] = (char) (green);
rawData[byteIndex+2] = (char) (blue);
byteIndex += 4;
}
ctx = CGBitmapContextCreate(rawData,
CGImageGetWidth( imageRef ),
CGImageGetHeight( imageRef ),
8,
CGImageGetBytesPerRow( imageRef ),
CGImageGetColorSpace( imageRef ),
kCGImageAlphaPremultipliedLast );
imageRef = CGBitmapContextCreateImage (ctx);
UIImage* rawImage = [UIImage imageWithCGImage:imageRef];
CGContextRelease(ctx);
self.workingImage = rawImage;
[self.imageView setImage:self.workingImage];
free(rawData);
}
但我没有成功。
答案 0 :(得分:1)
是的,您可以在其上绘制另一个黑色图层。请参阅下面我的代码,它将满足您的要求:
注意:请在您的代码中添加“testImage.png”图片,然后执行以下代码。
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize strTemp3;
- (void)viewDidLoad {
[super viewDidLoad];
[self testImageWrite];
[self addNewImageFromPath];
}
- (UIImage *)imageToDraw
{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 100), NO, [UIScreen mainScreen].scale);
UIImage *natureImage = [UIImage imageNamed:@"testImage"];
[natureImage drawInRect:CGRectMake(0, 0, 300, 100)];
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect rectangle = CGRectMake(75, 25, 150, 50);
CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1.0);
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
CGContextFillRect(context, rectangle);
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
}
- (NSString *)filePath
{
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"Image.png"];
}
- (void)testImageWrite
{
NSData *imageData = UIImagePNGRepresentation([self imageToDraw]);
NSError *writeError = nil;
BOOL success = [imageData writeToFile:[self filePath] options:0 error:&writeError];
if (!success || writeError != nil)
{
NSLog(@"Error Writing: %@",writeError.description);
}
}
-(void)addNewImageFromPath{
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 300, 100)];
imgView.image = [UIImage imageWithContentsOfFile:[self filePath]];
[self.view addSubview:imgView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
<强>输出:强>