//
// ViewController.m
// UIImage
//
// Created by HarrySun on 16/7/11.
// Copyright © 2016年 Mobby. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) UIImage *myImage;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 使用文件创建
// _myImage = [UIImage imageNamed:@"1"];
// 使用URL创建
// _myImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.kutx.cn/xiaotupian/icons/png/200803/20080327095245737.png"]]];
// // 通过文件加载指定路径下的文件内容获得新图片
// _myImage = [[UIImage alloc]initWithContentsOfFile:[NSString stringWithFormat:@"%@/Documents/ppp.png",NSHomeDirectory()]];
_myImage = [UIImage imageWithCGImage:[UIImage imageNamed:@"1"].CGImage scale:1 orientation:(UIImageOrientationLeft)]; // 创建的时候指定方法倍数以及旋转方向,scale是倍数,1代表和原图像尺寸一样,orientation 图片方向
// _myImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.kutx.cn/xiaotupian/icons/png/200803/20080327095245737.png"]]];
// CGImageRef cif=[_myImage CGImage];
// _myImage = [UIImage imageWithCGImage:cif]; //使用Quartz 2D对象创建UIImage
// 使用Core Graphics (静态方法)
// UIImage* myImage3 = [UIImage imageWithCGImage:cif];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 100, 100, 100)];
[self.view addSubview:imageView];
imageView.image = _myImage;
NSLog(@"%ld",(long)_myImage.imageOrientation); // 图片方向
NSLog(@"%f,%f",_myImage.size.width,_myImage.size.height); // 图片大小
NSLog(@"%f",_myImage.scale); // 图片的比例
// 根据给定的图片,从其指定区域截取一张新的图片
UIImage *image1 = [self getImageFromImage];
UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(50, 250, 100, 100)];
imageView2.backgroundColor = [UIColor redColor];
[self.view addSubview:imageView2];
imageView2.image = image1;
UIImage *image3 = [_myImage imageWithAlignmentRectInsets:(UIEdgeInsetsMake(50, 100, 100, 100))]; // 返回指定矩形区域内的图像
UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(50, 400, 100, 100)];
imageView3.image = image3;
[self.view addSubview:imageView3];
// ?
// UIImage *image4 = [UIImage animatedImageNamed:@"xian-1.png" duration:5];
//
// UIImageView *imageView4 = [[UIImageView alloc] initWithFrame:CGRectMake(200, 100, 100, 100)];
// imageView4.image = image4;
// [self.view addSubview:imageView4];
// --------- 下方设置动画 ---------
NSMutableArray *imageArray = [[NSMutableArray alloc] initWithCapacity:30];
for (int i = 1; i < 31; i ++) {
UIImage *aImage = [UIImage imageNamed:[NSString stringWithFormat:@"xian-%d",i]];
[imageArray addObject:aImage];
}
UIImage *image = [UIImage animatedImageWithImages:imageArray duration:2]; // 用一组图片创建一个动态图片
UIImageView *imgeView5 = [[UIImageView alloc] initWithFrame:CGRectMake(50, 550, 100, 100)];
imgeView5.image = image;
[self.view addSubview:imgeView5];
UIImage *image6 = [UIImage animatedResizableImageNamed:@"1.png" capInsets:UIEdgeInsetsMake(50, 200, 100, 100) resizingMode:UIImageResizingModeTile duration:0];
UIImageView *imageView6 = [[UIImageView alloc] initWithFrame:CGRectMake(200, 250, 100, 100)];
imageView6.image = image6;
[self.view addSubview:imageView6];
}
// 根据给定的图片,从其指定区域截取一张新的图片
-(UIImage *)getImageFromImage{
//大图bigImage
//定义myImageRect,截图的区域
CGRect myImageRect = CGRectMake(50.0, 100.0, 50.0, 50.0);
UIImage* bigImage= [UIImage imageNamed:@"1.png"];
CGImageRef imageRef = bigImage.CGImage;
CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);
CGSize size;
size.width = 50.0;
size.height = 50.0;
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, myImageRect, subImageRef);
UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
UIGraphicsEndImageContext();
return smallImage;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end