UI_UIImage

//

//  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:@"xian1.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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值