我正在处理需要图像像素操作的项目。我现在在做的是使用UIImagePicker
从设备库中接收UIImage
。然后我将它转换为CGImage
,获取RBG值并稍微改变它们。UIImage RBG值更改保存到设备库时
然后我将它转换回UIImage
并写出到磁盘。问题是当我从UIImagePicker
重新读取图像时,RBG值不一样。我已经验证之后的值是正确的我改变它们并且之前图像被实际写出。当我读回来时,像素值仅有不同,然后只有几个值。我很好奇,为什么会发生这种情况,有什么办法解决这个问题?我想要找回完全相同的RBG值。
这是一段代码,如果你想要特定的东西请让我知道。
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
// I only ever manipulate self.editingImage which is directly read from the photo library
// self.editingImage is a UIImage property
self.editingImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
self.imageView.image = self.editingImage;
if (self.option == EDITPIXELS) {
// After this method completes it automatically calls READPIXELS and the values are correct
// converts self.editingImage to a CGImage before editing pixels
[self editImage:self.editingImage];
} else if (self.option == READPIXELS) {
// converts self.editingImage to a CGImage before reading pixels, then logs RBG values
[self readImage:self.editingImage];
}
[self.imagePickerPopoverController dismissPopoverAnimated:YES];
}
编辑: 我使用category from here救我的形象,从类别代码:
-(void)saveImage:(UIImage*)image toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock {
//write the image data to the assets library (camera roll)
[self writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation
completionBlock:^(NSURL* assetURL, NSError* error) {
//error handling
if (error!=nil) {
completionBlock(error);
return;
}
//add the asset to the custom photo album
[self addAssetURL: assetURL
toAlbum:albumName
withCompletionBlock:completionBlock];
}];
}
我呼吁它的动作按钮:
- (IBAction)saveImage:(id)sender {
// Called before saving to verify the RBG values, they are correct
[self readImage:self.editingImage];
// saving image
[self.library saveImage:self.editingImage toAlbum:@"My Album" withCompletionBlock:^(NSError *error){}];
}
这是我用来编辑RBG值的代码:
+ (UIImage *) fromImage:(UIImage *)source toRedColors:(NSArray *)redColorArray {
// Code adapted from: http://brandontreb.com/image-manipulation-retrieving-and-updating-pixel-values-for-a-uiimage/
CGContextRef ctx;
CGImageRef imageRef = [source CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
byte *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);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
int byteIndex = 0;
for (NSNumber *n in redColorArray) {
rawData[byteIndex] = Clamp255([n integerValue]);
byteIndex += 4;
}
ctx = CGBitmapContextCreate(rawData,
CGImageGetWidth(imageRef),
CGImageGetHeight(imageRef),
8,
bytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorSpace);
imageRef = CGBitmapContextCreateImage (ctx);
UIImage* rawImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CGContextRelease(ctx);
free(rawData);
return rawImage;
}
读取值几乎使用完全相同的代码,除了将值存储到数组中并返回RBG值的数组(我不会返回其中一些RBG值)。因此,而不是:
rawData[byteIndex] = Clamp255([n integerValue]);
我用:
[myMutableArray addObject:@(rawData[byteIndex])];
对不起,但我没有看到你在任何地方保存图片。当您导入图片时,您有图片的副本,而不是实际的图片,您需要将实际图片保存到照片库中,以使其不会自动完成。如果我错了,并且实际上将照片保存在照片库中,请发送代码 – Radu 2013-05-10 14:36:42
您保存的格式(例如jpg,png)? – bobnoble 2013-05-10 14:37:45
发布包含读取RGB值和编辑RGB值的代码 – Divyu 2013-05-10 14:44:55