我正在處理需要圖像像素操作的項目。我現在在做的是使用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