归朴子兴 發表於 2024-7-6 20:17:00

iOS开发-图片UIImage

<p><code>UIImage</code> 和 <code>UIImageView</code> 是 iOS 开发中常用的两个类,分别用于表示图像数据和显示图像。</p>
<h3 id="uiimage">UIImage</h3>
<p><code>UIImage</code> 是一个表示图像数据的类,可以从文件、数据、图像资源库等加载图像。<code>UIImage</code> 支持多种图像格式,包括 PNG、JPEG、GIF 等。</p>
<h4 id="创建-uiimage">创建 UIImage</h4>
<ol>
<li>
<p><strong>从文件创建</strong></p>
<pre><code class="language-objective-c">UIImage *image = ;
</code></pre>
</li>
<li>
<p><strong>从数据创建</strong></p>
<pre><code class="language-objective-c">NSData *imageData = ;
UIImage *image = ;
</code></pre>
</li>
<li>
<p><strong>从 URL 创建</strong></p>
<pre><code class="language-objective-c">NSURL *imageUrl = ;
NSData *imageData = ;
UIImage *image = ;
</code></pre>
</li>
<li>
<p><strong>从颜色创建</strong></p>
<pre><code class="language-objective-c">UIColor *color = ;
CGSize size = CGSizeMake(100, 100);
UIGraphicsBeginImageContext(size);
;
UIRectFill(CGRectMake(0, 0, size.width, size.height));
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
</code></pre>
</li>
</ol>
<h4 id="处理-uiimage">处理 UIImage</h4>
<ul>
<li>
<p><strong>获取图像尺寸</strong></p>
<pre><code class="language-objective-c">CGSize imageSize = image.size;
</code></pre>
</li>
<li>
<p><strong>获取图像的缩放比例</strong></p>
<pre><code class="language-objective-c">CGFloat scale = image.scale;
</code></pre>
</li>
<li>
<p><strong>保存图像到文件</strong></p>
<pre><code class="language-objective-c">NSData *imageData = UIImagePNGRepresentation(image);
;
</code></pre>
</li>
</ul>
<h3 id="uiimageview">UIImageView</h3>
<p><code>UIImageView</code> 是一个用于显示图像的视图类。它可以显示 <code>UIImage</code> 对象,并提供了一些方便的方法来调整图像的显示方式。</p>
<ul>
<li>
<p>创建 UIImageView</p>
<pre><code class="language-objective-c">UIImageView *imageView = [ initWithImage:image];
imageView.frame = CGRectMake(50, 50, 100, 100);
</code></pre>
</li>
</ul>
<h4 id="配置-uiimageview">配置 UIImageView</h4>
<ul>
<li>
<p><strong>设置图像</strong></p>
<pre><code class="language-objective-c">imageView.image = image;
</code></pre>
</li>
<li>
<p><strong>内容模式</strong></p>
<p><code>UIImageView</code> 提供了多种内容模式,用于控制图像如何在视图中显示:</p>
<pre><code class="language-objective-c">imageView.contentMode = UIViewContentModeScaleAspectFit;// 保持比例适应视图
imageView.contentMode = UIViewContentModeScaleAspectFill; // 保持比例填充视图,可能会裁剪图像
imageView.contentMode = UIViewContentModeCenter;          // 居中显示图像
</code></pre>
</li>
<li>
<p><strong>设置边框和圆角</strong></p>
<pre><code class="language-objective-c">imageView.layer.borderColor = .CGColor;
imageView.layer.borderWidth = 2.0;
imageView.layer.cornerRadius = 10.0;
imageView.clipsToBounds = YES;
</code></pre>
</li>
</ul>
<h4 id="动画-uiimageview">动画 UIImageView</h4>
<ul>
<li>
<p><strong>逐帧动画</strong></p>
<p><code>UIImageView</code> 可以通过设置 <code>animationImages</code> 属性来播放逐帧动画:</p>
<pre><code class="language-objective-c">imageView.animationImages = @;
imageView.animationDuration = 1.0;// 动画时长
imageView.animationRepeatCount = 0; // 无限循环
;
</code></pre>
</li>
</ul>
<h3 id="使用示例">使用示例</h3>
<p>以下是一个完整的示例,展示了如何使用 <code>UIImage</code> 和 <code>UIImageView</code>:</p>
<h4 id="viewcontrollerh">ViewController.h</h4>
<pre><code class="language-objective-c">#import &lt;UIKit/UIKit.h&gt;

@interface ViewController : UIViewController

@end
</code></pre>
<h4 id="viewcontrollerm">ViewController.m</h4>
<pre><code class="language-objective-c">#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad {
    ;
    self.view.backgroundColor = ;
   
    // 创建 UIImage 对象
    UIImage *image = ;
   
    // 创建 UIImageView 对象并设置图像
    UIImageView *imageView = [ initWithImage:image];
    imageView.frame = CGRectMake(50, 50, 200, 200);
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.layer.borderColor = .CGColor;
    imageView.layer.borderWidth = 2.0;
    imageView.layer.cornerRadius = 10.0;
    imageView.clipsToBounds = YES;
    ;
   
    // 动画 UIImageView
    UIImage *image1 = ;
    UIImage *image2 = ;
    UIImage *image3 = ;
    UIImageView *animatedImageView = [ initWithFrame:CGRectMake(50, 300, 200, 200)];
    animatedImageView.animationImages = @;
    animatedImageView.animationDuration = 1.0;
    animatedImageView.animationRepeatCount = 0;
    ;
    ;
}

@end
</code></pre><br><br>
来源:https://www.cnblogs.com/jianqiu/p/18287683
頁: [1]
查看完整版本: iOS开发-图片UIImage