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ć?

Author: Duck, 2011-12-08

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!).

 130
Author: changx,
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]];
 31
Author: Chris Vander Mey,
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)

 23
Author: Juraj Antas,
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.

 13
Author: JVillella,
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)
}
 12
Author: Etan,
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)
 6
Author: Daniel Storm,
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

 6
Author: user924,
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;
 1
Author: Laurent Crivello,
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