Jak przekonwertować UIImage na CIImage i vice versa
Próbuję uzyskać CIImage
z ImageView ten wyświetlacz na ekranie.
UIImage *image = myImageView.image;
Konwertuj obraz UIImage
na CIImage
.
CIImage *cImage = [....];
Jak to zrobić?
8 answers
CIImage *ciImage = [UIImage imageNamed:@"test.png"].CIImage;
UIImage *uiImage = [[UIImage alloc] initWithCIImage:ciImage];
Aby naprawić przypadek, w którym myUIImage.CIImage
zwraca nil
Jak [UIImageView image]
, możesz zamiast tego wykonać [CIImage imageWithCGImage:myUIImage.CGImage]
- Dylan Hand
Wersja Swift:
let ciImage = UIImage(named: "test.png")!.ciImage
let uiImage = UIImage(ciImage: ciImage)
Aby naprawić przypadek, w którym myUIImage.ciImage
zwraca nil
, Jak możesz zamiast tego zrobić CIImage(cgImage: myUIImage!.cgImage!)
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-06-14 15:43:25
Chociaż odpowiedź Changxing Wang jest faktycznie poprawna, Apple mówi, że UIImage.CIImage nie zawsze zadziała. W szczególności z PocketCoreImage:
// Create a CIImage from the _inputImage. While UIImage has a property returning
// a CIImage representation of it, there are cases where it will not work. This is the
// most compatible route.
_filteredImage = [[CIImage alloc] initWithCGImage:_inputImage.CGImage options:nil];
Apple również używa następujących, co nie działa dla mnie:
[UIImage imageWithCIImage:_filteredImage]
Moja osobista implementacja, zaadaptowana z Ten post działa dla mnie:
// result is a CIImage, output of my filtering.
// returnImage is (obviously) a UIImage that I'm going to return.
CIContext *context = [CIContext contextWithOptions:nil];
UIImage *returnImage =
[UIImage imageWithCGImage:[context createCGImage:result fromRect:result.extent]];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:09:36
Jest to najbardziej zgodny sposób:
UIImage* u =[UIImage imageNamed:@"image.png"];
CIImage* ciimage = [[CIImage alloc] initWithCGImage:u.CGImage];
Teraz użyj ciimage...
I wersja Swift:
let i1 = UIImage(named: "testImage")
if let cgi1 = i1?.cgImage {
let ci = CIImage(cgImage: cgi1)
//use ci
}
(poprawiona literówka w instrukcji if let)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-09-10 12:22:27
Oto pełna próbka kodu, którą wykorzystałem w moich projektach.
- (UIImage *)makeUIImageFromCIImage:(CIImage *)ciImage {
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgImage = [context createCGImage:ciImage fromRect:[ciImage extent]];
UIImage* uiImage = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
return uiImage;
}
CIImage
jest w zasadzie przepis na obraz. Napotkasz problemy, jeśli spróbujesz bezpośrednio przekonwertować z CIImage
na UIImage
. Na przykład wywołanie
NSData * UIImageJPEGRepresentation (
UIImage *image,
CGFloat compressionQuality
);
Zwróci nil
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-18 13:54:43
func toCIImage(image: UIImage) -> CIImage {
return image.CIImage ?? CIImage(CGImage: image.CGImage!)
}
func toUIImage(image: CIImage) -> UIImage {
return UIImage(CIImage: image)
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-07 19:04:09
Swift przykład changx ' s odpowiedź:
guard let ciimage = CIImage(image: uiimage) else { return }
let image = UIImage(CIImage: ciimage)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:46:54
Swift 3/4
UIImage > CIImage
let ciimage = CIImage(image: image)
Https://developer.apple.com/documentation/uikit/uiimage/1624090-init
CIImage > UIImage
let image = UIImage(ciImage: ciimage)
Https://developer.apple.com/documentation/coreimage/ciimage/1624119-init
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-01 14:22:10
Podczas konwersji UIImage na CIImage, miałem 2 oddzielne przypadki, w których czasami jest zero, a czasami nie. Poniższy kod rozwiązał to:
CIImage *ciImage;
ciImage=[[CIImage alloc] initWithImage:imageToConvert];
if (ciImage==nil)
ciImage=imageToConvert.CIImage;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-27 18:53:19