【问题标题】:Setting a UIImage border设置 UIImage 边框
【发布时间】:2011-04-17 07:58:24
【问题描述】:

有没有办法为 UIImage 设置边框。我知道如何为 UIImageView 设置它,但我的问题是我在 UIImageview 中加载的 UIImage 不会与 UIImageView 具有相同的大小或纵横比。因此,我一直保持 UIImageView 模式以适应方面。现在为 UIImageView 提供边框将给整个 UIImageView 加上边框,而不仅仅是 UIImage,而且当 UIImage 的大小或纵横比与 UIV 不同时,这看起来不太好。

帮助?

【问题讨论】:

    标签: iphone objective-c cocoa-touch xcode uiimage


    【解决方案1】:
    #import <QuartzCore/CALayer.h>
    
    
    UIImageView *imageView = [UIImageView alloc]init];
    imageView.layer.masksToBounds = YES;
    imageView.layer.borderColor = [UIColor blackColor].CGColor;
    imageView.layer.borderWidth = 1;
    imageView.layer.cornerRadius = 10; //optional
    

    【讨论】:

      【解决方案2】:
      image with border
      
      +(UIImage*)imageWithBorderFromImage:(UIImage*)source
      {
      
          CGSize size = [source size];
      
          UIGraphicsBeginImageContext(size);
          CGRect rect = CGRectMake(0, 0, size.width, size.height);
          [source drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];
      
          CGContextRef context = UIGraphicsGetCurrentContext();
          CGContextSetRGBStrokeColor(context,(255/255.f),(255/255.f),(255/255.f), 1.0);
          CGContextStrokeRect(context, rect);
          UIImage *testImg =  UIGraphicsGetImageFromCurrentImageContext();
          UIGraphicsEndImageContext();
          return testImg;
      }
      

      【讨论】:

      • 如何设置边框宽度?
      【解决方案3】:

      这个问题有一些解决方案:How can i take an UIImage and give it a black border?

      【讨论】:

      • 这里有两个问题。另一个问题是关于直接渲染 UIImage,而不是使用 UIImageView。此外,最好的解决方案是在 UIImageView 的框架周围放置边框,而不是在 UIImageView 内放置适合宽高比的图像。
      • 其实另一个问题也差不多,只是那里流行的答案在这里没有用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-26
      • 1970-01-01
      • 2018-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-26
      相关资源
      最近更新 更多