2013-04-08 2 views
9

감사합니다.CIFIlter를 적용한 후 UIImageView 콘텐츠 모드를 설정하십시오.

여기 여기 _vignette에 내 코드

CIImage *result = _vignette.outputImage; 
self.mainImageView.image = nil; 
//self.mainImageView.contentMode = UIViewContentModeScaleAspectFit; 
self.mainImageView.image = [UIImage imageWithCIImage:result]; 
self.mainImageView.contentMode = UIViewContentModeScaleAspectFit; 

의 제대로 필터를 설정 및 이미지 효과는 제대로 이미지에 적용됩니다.

해상도가 500x375 인 소스 이미지를 사용하고 있습니다. 내 imageView 거의 아이폰 화면의 해상도가 있습니다. 따라서 스트레칭을 피하기 위해 AspectFit을 사용합니다.

그러나 결과 이미지를 내 이미지 뷰에 다시 할당 할 때 효과를 적용한 후에는 이미지가 흐릿 해집니다. 어떤 UIViewContentMode을 사용 하든지 상관 없습니다. 작동하지 않습니다. 내가 준 필터에 관계없이 항상 ScaleToFill을 적용하는 것 같습니다.

왜 이런 생각입니까? 모든 제안은 높이 평가됩니다.

+0

은 기본적으로 중복 http://stackoverflow.com/questions/9094058/ Creating-uiimage-from-ciimage – matt

답변

20

(1) 가로 세로 맞춤 않습니다. 이미지 늘이기 - 맞 춥니 다. 이미지를 전혀 늘리지 않으려면 Center (예 :)를 사용하십시오.

(2) imageWithCIImage은 매우 이상한 짐승, CGImage를 기반으로하지 않는 UIImage를 제공하므로 레이어 표시의 일반적인 규칙에 영향을받지 않습니다. 정말 CIImage를 감싸는 얇은 포장지 일뿐입니다. CGImage를 통해 CIFilter 출력을 UIImage로 변환 (렌더링)해야합니다. 따라서 실제로 비트 (CGImage, 비트 맵)가있는 UIImage를 제공합니다. 어떤 시점에서 당신은 당신의 CIFilter의 출력에서 ​​CGImageRef를 생성하는 CIContext createCGImage:fromRect:를 호출해야하고,있는 UIImage로 그에 합격, 즉

http://www.apeth.com/iOSBook/ch15.html#_cifilter_and_ciimage

: 내 논의는 여기에 당신에게 보여 코드를 제공합니다. 그렇게 할 때까지 필터 조작을 실제 UIImage로 출력하지 않습니다.

또는 imageWithCIImage에서 그래픽 컨텍스트로 이미지를 그릴 수 있습니다. 예를 들어 이미지 그래픽 컨텍스트에 그려 넣은 다음 인 이미지를 이미지로 사용할 수 있습니다.

수 없으며 직접 imageWithCIImage의 이미지를 표시합니다. 그것은 이미지가 아니기 때문입니다! 기본 비트 맵 (CGImage)이 없습니다. 거기에는 아무 것도 없습니다. 모두 그것이 이미지를 파생시키기위한 일련의 CIFilter 명령입니다.

+0

감사합니다!나는 단지 종횡비를 유지하기를 원합니다. 종횡비를 유지하면서 늘리는 것이 좋습니다. 나는 나의 질문을 편집했다. 항상 aspectFit이 적용되지 않았습니다. 그것은'saceltoFill'이었다. 설정 한 콘텐츠 모드에 관계없이 UI는 업데이트되지 않습니다. 그것은 scaleToFill로 유지됩니다. 하지만 aspectFit을 설정 한 후에 속성을 확인하면 올바르게 설정됩니다 (마지막 코드 줄). 그러나 ImageView는 업데이트되지 않았습니다. 왜 그런가? 간단히 imageView에 이미지를로드하고 가로 세로 비율을 변경하지 않고 CIFilter를 적용하기 만하면됩니다. 내 요구 사항은 간단합니다. 하지만 내 소스 이미지는 다른 해상도와 비율이 될 수 있습니다. – sleepwalkerfx

+1

계속 말씀 드리지 만, 당신은 나를 믿지 않는 것처럼 보입니다. 이미지 뷰의 이미지를'imageWithCIImage :'로 생성 된 UIImage로 설정하는 것은 작동하지 않습니다. – matt

+0

당신이 옳았다 :) 나는 방금 언급 한 방법으로 이것을 테스트했다. 매력처럼 일했다. 고마워요! – sleepwalkerfx

1

나는 하루 종일 이것을 보냈다. 오리엔테이션에 문제가있어 품질이 떨어졌습니다.

카메라를 사용하여 이미지를 스냅 한 후이를 수행합니다.

NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; 
       UIImage *image = [[UIImage alloc] initWithData:imageData]; 

이렇게하면 회전 문제가 발생하므로이 작업을 수행해야합니다. 내 필터를 적용 할 때

UIImage *scaleAndRotateImage(UIImage *image) 
{ 
    int kMaxResolution = image.size.height; // Or whatever 

    CGImageRef imgRef = image.CGImage; 

    CGFloat width = CGImageGetWidth(imgRef); 
    CGFloat height = CGImageGetHeight(imgRef); 

    CGAffineTransform transform = CGAffineTransformIdentity; 
    CGRect bounds = CGRectMake(0, 0, width, height); 
    if (width > kMaxResolution || height > kMaxResolution) { 
     CGFloat ratio = width/height; 
     if (ratio > 1) { 
      bounds.size.width = kMaxResolution; 
      bounds.size.height = bounds.size.width/ratio; 
     } 
     else { 
      bounds.size.height = kMaxResolution; 
      bounds.size.width = bounds.size.height * ratio; 
     } 
    } 

    CGFloat scaleRatio = bounds.size.width/width; 
    CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef)); 
    CGFloat boundHeight; 
    UIImageOrientation orient = image.imageOrientation; 
    switch(orient) { 

     case UIImageOrientationUp: //EXIF = 1 
      transform = CGAffineTransformIdentity; 
      break; 

     case UIImageOrientationUpMirrored: //EXIF = 2 
      transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0); 
      transform = CGAffineTransformScale(transform, -1.0, 1.0); 
      break; 

     case UIImageOrientationDown: //EXIF = 3 
      transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height); 
      transform = CGAffineTransformRotate(transform, M_PI); 
      break; 

     case UIImageOrientationDownMirrored: //EXIF = 4 
      transform = CGAffineTransformMakeTranslation(0.0, imageSize.height); 
      transform = CGAffineTransformScale(transform, 1.0, -1.0); 
      break; 

     case UIImageOrientationLeftMirrored: //EXIF = 5 
      boundHeight = bounds.size.height; 
      bounds.size.height = bounds.size.width; 
      bounds.size.width = boundHeight; 
      transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width); 
      transform = CGAffineTransformScale(transform, -1.0, 1.0); 
      transform = CGAffineTransformRotate(transform, 3.0 * M_PI/2.0); 
      break; 

     case UIImageOrientationLeft: //EXIF = 6 
      boundHeight = bounds.size.height; 
      bounds.size.height = bounds.size.width; 
      bounds.size.width = boundHeight; 
      transform = CGAffineTransformMakeTranslation(0.0, imageSize.width); 
      transform = CGAffineTransformRotate(transform, 3.0 * M_PI/2.0); 
      break; 

     case UIImageOrientationRightMirrored: //EXIF = 7 
      boundHeight = bounds.size.height; 
      bounds.size.height = bounds.size.width; 
      bounds.size.width = boundHeight; 
      transform = CGAffineTransformMakeScale(-1.0, 1.0); 
      transform = CGAffineTransformRotate(transform, M_PI/2.0); 
      break; 

     case UIImageOrientationRight: //EXIF = 8 
      boundHeight = bounds.size.height; 
      bounds.size.height = bounds.size.width; 
      bounds.size.width = boundHeight; 
      transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0); 
      transform = CGAffineTransformRotate(transform, M_PI/2.0); 
      break; 

     default: 
      [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"]; 

    } 

    UIGraphicsBeginImageContext(bounds.size); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) { 
     CGContextScaleCTM(context, -scaleRatio, scaleRatio); 
     CGContextTranslateCTM(context, -height, 0); 
    } 
    else { 
     CGContextScaleCTM(context, scaleRatio, -scaleRatio); 
     CGContextTranslateCTM(context, 0, -height); 
    } 

    CGContextConcatCTM(context, transform); 

    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef); 
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return imageCopy; 
} 

그런 다음, 나는 (이 스레드에 특별한 감사와 함께 - 특별히 매트 및 Wex)이 작업을 수행

-(UIImage*)processImage:(UIImage*)image { 

      CIImage *inImage = [CIImage imageWithCGImage:image.CGImage]; 

      CIFilter *filter = [CIFilter filterWithName:@"CIColorControls" keysAndValues: 
         kCIInputImageKey, inImage, 
         @"inputContrast", [NSNumber numberWithFloat:1.0], 
         nil]; 

    UIImage *outImage = [filter outputImage]; 

//Juicy bit 
      CGImageRef cgimageref = [[CIContext contextWithOptions:nil] createCGImage:outImage fromRect:[outImage extent]]; 

      return [UIImage imageWithCGImage:cgimageref]; 
     } 
관련 문제