iOS開發(fā)-圖片UIImage
UIImage 和 UIImageView 是 iOS 開發(fā)中常用的兩個類,分別用于表示圖像數(shù)據(jù)和顯示圖像。
UIImage
UIImage 是一個表示圖像數(shù)據(jù)的類,可以從文件、數(shù)據(jù)、圖像資源庫等加載圖像。UIImage 支持多種圖像格式,包括 PNG、JPEG、GIF 等。
創(chuàng)建 UIImage
-
從文件創(chuàng)建
UIImage *image = [UIImage imageNamed:@"exampleImage"]; -
從數(shù)據(jù)創(chuàng)建
NSData *imageData = [NSData dataWithContentsOfFile:@"path/to/image"]; UIImage *image = [UIImage imageWithData:imageData]; -
從 URL 創(chuàng)建
NSURL *imageUrl = [NSURL URLWithString:@"https://example.com/image.png"]; NSData *imageData = [NSData dataWithContentsOfURL:imageUrl]; UIImage *image = [UIImage imageWithData:imageData]; -
從顏色創(chuàng)建
UIColor *color = [UIColor redColor]; CGSize size = CGSizeMake(100, 100); UIGraphicsBeginImageContext(size); [color setFill]; UIRectFill(CGRectMake(0, 0, size.width, size.height)); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
處理 UIImage
-
獲取圖像尺寸
CGSize imageSize = image.size; -
獲取圖像的縮放比例
CGFloat scale = image.scale; -
保存圖像到文件
NSData *imageData = UIImagePNGRepresentation(image); [imageData writeToFile:@"path/to/save.png" atomically:YES];
UIImageView
UIImageView 是一個用于顯示圖像的視圖類。它可以顯示 UIImage 對象,并提供了一些方便的方法來調(diào)整圖像的顯示方式。
-
創(chuàng)建 UIImageView
UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; imageView.frame = CGRectMake(50, 50, 100, 100);
配置 UIImageView
-
設(shè)置圖像
imageView.image = image; -
內(nèi)容模式
UIImageView提供了多種內(nèi)容模式,用于控制圖像如何在視圖中顯示:imageView.contentMode = UIViewContentModeScaleAspectFit; // 保持比例適應(yīng)視圖 imageView.contentMode = UIViewContentModeScaleAspectFill; // 保持比例填充視圖,可能會裁剪圖像 imageView.contentMode = UIViewContentModeCenter; // 居中顯示圖像 -
設(shè)置邊框和圓角
imageView.layer.borderColor = [UIColor blackColor].CGColor; imageView.layer.borderWidth = 2.0; imageView.layer.cornerRadius = 10.0; imageView.clipsToBounds = YES;
動畫 UIImageView
-
逐幀動畫
UIImageView可以通過設(shè)置animationImages屬性來播放逐幀動畫:imageView.animationImages = @[image1, image2, image3]; imageView.animationDuration = 1.0; // 動畫時長 imageView.animationRepeatCount = 0; // 無限循環(huán) [imageView startAnimating];
使用示例
以下是一個完整的示例,展示了如何使用 UIImage 和 UIImageView:
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
ViewController.m
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// 創(chuàng)建 UIImage 對象
UIImage *image = [UIImage imageNamed:@"exampleImage"];
// 創(chuàng)建 UIImageView 對象并設(shè)置圖像
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(50, 50, 200, 200);
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.layer.borderColor = [UIColor blackColor].CGColor;
imageView.layer.borderWidth = 2.0;
imageView.layer.cornerRadius = 10.0;
imageView.clipsToBounds = YES;
[self.view addSubview:imageView];
// 動畫 UIImageView
UIImage *image1 = [UIImage imageNamed:@"frame1"];
UIImage *image2 = [UIImage imageNamed:@"frame2"];
UIImage *image3 = [UIImage imageNamed:@"frame3"];
UIImageView *animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 300, 200, 200)];
animatedImageView.animationImages = @[image1, image2, image3];
animatedImageView.animationDuration = 1.0;
animatedImageView.animationRepeatCount = 0;
[animatedImageView startAnimating];
[self.view addSubview:animatedImageView];
}
@end

浙公網(wǎng)安備 33010602011771號