【问题标题】:How to fill the background with image in landscape in IOS?如何在IOS中用横向图像填充背景?
【发布时间】:2013-04-15 13:08:46
【问题描述】:

我正在做的是使用从 UIImagePickerController 返回的图像填充视图的背景。纵向模式下图像填充良好;但是,当在横向模式下填充为背景时,图像会重复,但我不知道为什么会发生这种情况。这是我用来调整图像大小的私有方法。

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize landscape:(BOOL)landscape {
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

调用此方法时,newsize 参数等于视图边界大小(self.view.bounds.size)。在视图转换为横向后访问大小,但图像不正确。

这是从 UIImagePickerController 获取图像后立即调用的代码。

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    if (image.size.width > image.size.height) {
        self.view.transform = CGAffineTransformRotate(self.view.transform, M_PI_2);
        self.composition.landscapemode = YES;
    } else {
        self.composition.landscapemode = NO;
    }
    self.composition.image = [NewCompositionViewController imageWithImage:image scaledToSize:self.view.bounds.size landscape:self.composition.landscapemode];
    self.view.backgroundColor = [UIColor colorWithPatternImage:self.composition.image];
    [self dismissViewControllerAnimated:YES completion:nil];
}

【问题讨论】:

    标签: ios objective-c cocoa-touch orientation uiimagepickercontroller


    【解决方案1】:

    [UIColor colorWithPatternImage:] 用于平铺图像,因此它的行为应如此。

    我建议创建一个具有屏幕大小框架的 UIImageView,为其设置图像,并将其添加为子视图:

    UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:self.view.frame];
    [backgroundImage setImage:self.composition.image];
    
    // choose best mode that works for you
    [backgroundImage setContentMode:UIViewContentModeScaleAspectFill];  
    
    [self.view insertSubview:backgroundImage atIndex:0];
    //OR
    [self.view addSubview:backgroundImage];
    [self.view sendSubviewToBack:backgroundImage];
    

    添加后,您可以旋转它并尝试自动调整遮罩大小,以确保它在所有方向上都能正确显示。确切的方法取决于您是否使用自动布局。

    【讨论】:

      【解决方案2】:

      UIViewContentModeScaleAspectFill 在这里可能比UIViewContentModeScaleAspectFit 更合适,因为图像正在填充背景视图。 AspectFit 将保持图像的纵横比并使整个图像适合空间,这可能会使部分视图透明。 AspectFill 也保持纵横比,但会填充整个视图并剪切与视图边界不匹配的图像的任何部分。

      【讨论】:

        【解决方案3】:

        通过结合一些 AVFoundation 和 UIKit API,我已经能够将“aspect fit” UIImage 应用到 UIView 背景。这是一个例子:

        UIImage *image = [UIImage imageWithContentsOfFile:self.desiredBackgroundImageFilePathString];
        
        UIGraphicsBeginImageContext(self.drawingImage.frame.size);
        [image drawInRect:AVMakeRectWithAspectRatioInsideRect(image.size, self.drawingImage.bounds)];
        image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        self.drawingImage.backgroundColor = [UIColor colorWithPatternImage:image];
        

        这需要几个简单但重要的步骤:

        1. 从文件(或其他)生成 UIImage。
        2. 使用 UIGraphicsBeginImageContext() 定义图像的上下文(背景所需的 UIView)。
        3. 结合使用 drawInRect 和 AVMakeRectWithAspectRatioInsideRect 来缩放图像。为 AVMakeRect...() 提供图像的 .size 和目标 UIView 的边界。
        4. 将调整大小的图像应用于所需的图像上下文。
        5. 使用 colorWithPatternImage 将您现在调整大小的图像应用到目标 UIView 的 .backgroundColor。

        我能够使用此代码交换具有横向和纵向纵横比的图像,而不会出现对齐或剪切问题。

        【讨论】:

          猜你喜欢
          • 2015-10-29
          • 1970-01-01
          • 1970-01-01
          • 2016-02-12
          • 2019-01-28
          • 2011-12-26
          • 2018-10-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多