代码之家  ›  专栏  ›  技术社区  ›  GeneCode

带有自动布局约束的UIScrollView中的UIImageView出现混乱

  •  0
  • GeneCode  · 技术社区  · 6 年前

    谢谢你的关注。

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

    _vignette 正确设置过滤器,图像效果正确应用于图像。

    我使用的是分辨率为500x375的源图像。我的imageView几乎有iPhone屏幕的分辨率。所以为了避免拉伸,我使用AspectFit。

    UIViewContentMode 我用。它不起作用。这似乎总是适用的 ScaleToFill

    知道为什么会这样吗?非常感谢您的任何建议。

    0 回复  |  直到 11 年前
        1
  •  24
  •   matt    10 年前

    (1) 方面匹配 拉伸图像-以适合。如果您根本不想拉伸图像,请使用“中心”(例如)。

    imageWithCIImage 给你一个非常奇怪的野兽,一个不基于CGImage的UIImage,因此不受图层显示的正常规则的影响。它实际上只是CIImage的一个薄薄的包装,这不是您想要的。您必须通过CGImage将CIFilter输出转换(呈现)为UIImage,从而为您提供实际具有某些位的UIImage(CGImage,位图)。我在这里的讨论为您提供了演示以下内容的代码:

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

    createCGImage:fromRect: 从CIFilter的输出生成CGImageRef,并将其传递到UIImage。在此之前,过滤器操作的输出不会是真实的UIImage。

    图像与ciimage 那个

    你呢 不能 直接地那是因为它不是图像!它没有基础位图(CGImage)。那里没有。它只是一组用于导出图像的CIFilter指令。

        2
  •  2
  •   Tamás Sengel RIJO JOSEPH    6 年前

    这是斯威夫特的答案,灵感来自 this question the solution by user1951992

    let imageView = UIImageView(frame: CGRect(x: 100, y: 200, width: 100, height: 50))
    imageView.contentMode = .scaleAspectFit
    
    //just an example for a CIFilter
    //NOTE: we're using implicit unwrapping here because we're sure there is a filter
    //(and an image) named exactly this
    let filter = CIFilter(name: "CISepiaTone")!
    let image = UIImage(named: "image")!
    filter.setValue(CIImage(image: image), forKey: kCIInputImageKey)
    filter.setValue(0.5, forKey: kCIInputIntensityKey)
    
    guard let outputCGImage = filter?.outputImage,
        let outputImage = CIContext().createCGImage(outputCGImage, from: outputCGImage.extent) else {
            return
    }
    
    imageView.image = UIImage(cgImage: outputImage)
    
        3
  •  1
  •   user1951992 user1951992    10 年前

    我只是花了一整天在这上面。我遇到了定向问题,随之而来的是质量低劣的输出。

    在使用相机拍摄图像后,我会这样做。

    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;  
    }
    

        -(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];
            }