• Quartz2D练习 -- 裁剪图片分类


    Main.storyboard

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="5053" systemVersion="13D65" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" initialViewController="vXZ-lx-hvc">
        <dependencies>
            <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/>
        </dependencies>
        <scenes>
            <!--View Controller-->
            <scene sceneID="ufC-wZ-h7g">
                <objects>
                    <viewController id="vXZ-lx-hvc" customClass="LWTViewController" sceneMemberID="viewController">
                        <view key="view" contentMode="scaleToFill" id="kh9-bI-dsS">
                            <rect key="frame" x="0.0" y="0.0" width="320" height="480"/>
                            <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                            <subviews>
                                <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" id="mkN-c5-tVD">
                                    <rect key="frame" x="0.0" y="0.0" width="320" height="480"/>
                                    <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                                </imageView>
                            </subviews>
                            <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
                        </view>
                        <connections>
                            <outlet property="imageView" destination="mkN-c5-tVD" id="oa0-4P-LEN"/>
                        </connections>
                    </viewController>
                    <placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
                </objects>
            </scene>
        </scenes>
        <simulatedMetricsContainer key="defaultSimulatedMetrics">
            <simulatedStatusBarMetrics key="statusBar"/>
            <simulatedOrientationMetrics key="orientation"/>
            <simulatedScreenMetrics key="destination"/>
        </simulatedMetricsContainer>
    </document>
    View Code

    LWTViewController.h

    #import <UIKit/UIKit.h>
    
    @interface LWTViewController : UIViewController
    
    @end
    View Code

    LWTViewController.m

     1 //
     2 //  LWTViewController.m
     3 //  Quartz2D练习 -- 裁剪图片分类
     4 //
     5 //  Created by apple on 14-6-10.
     6 //  Copyright (c) 2014年 lwt. All rights reserved.
     7 //
     8 
     9 #import "LWTViewController.h"
    10 #import "UIImage+cutImageRound.h"
    11 
    12 @interface LWTViewController ()
    13 @property (weak, nonatomic) IBOutlet UIImageView *imageView;
    14 
    15 @end
    16 
    17 @implementation LWTViewController
    18 
    19 - (void)viewDidLoad
    20 {
    21     [super viewDidLoad];
    22     // Do any additional setup after loading the view, typically from a nib.
    23     
    24     // 获取要裁剪图片
    25     UIImage *newImage = [UIImage imageWithImageName:@"scene" margin:20 frameColor:[UIColor magentaColor]];
    26     // 测试图片效果
    27     // imageView的size等于图片的size
    28     self.imageView.frame = CGRectMake(0, 0, newImage.size.width, newImage.size.height);
    29     self.imageView.image = newImage;
    30     
    31     //保存图片
    32     // 将图片传承二进制数据
    33     NSData *data = UIImagePNGRepresentation(newImage);
    34     // 保存图片的地址
    35     NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"cutImage.png"];
    36     // 保存图片
    37     [data writeToFile:path atomically:YES];
    38     
    39     
    40 }
    41 @end
    View Code

    UIImage+cutImageRound.h

    #import <UIKit/UIKit.h>
    
    @interface UIImage (cutImageRound)
    /**
     *  将图片裁剪成圆形
     *
     *  @param imageName  需要裁剪的图片
     *  @param margin     相框的尺寸
     *  @param frameColor 相框的背景颜色
     *
     *  @return 返回裁剪好的图片
     */
    + (UIImage *)imageWithImageName:(NSString *)imageName margin:(CGFloat)margin frameColor:(UIColor *)frameColor;
    
    @end
    View Code

    UIImage+cutImageRound.m

    //
    //  UIImage+cutImageRound.m
    //  Quartz2D练习 -- 裁剪图片分类
    //
    //  Created by apple on 14-6-10.
    //  Copyright (c) 2014年 lwt. All rights reserved.
    //
    
    #import "UIImage+cutImageRound.h"
    
    @implementation UIImage (cutImageRound)
    
    + (UIImage *)imageWithImageName:(NSString *)imageName margin:(CGFloat)margin frameColor:(UIColor *)frameColor
    {
        // 0. 加载原有图片
        UIImage *image = [UIImage imageNamed:imageName];
        
        CGFloat imageW = image.size.width;
        CGFloat imageH = image.size.height;
        
        CGFloat graphW = imageW + 2 * margin;
        CGFloat graphH = imageH + 2 * margin;
        
        // 创建bigmap图形上下文
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(graphW, graphH), NO, 0);
        
        // 获取图形上下文
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        
        // 绘制背景大圆
        CGFloat bigX = 0;
        CGFloat bigY = 0;
        CGFloat bigW = graphW;
        CGFloat bigH = graphH;
        CGContextAddEllipseInRect(ctx, CGRectMake(bigX, bigY, bigW, bigH));
        [frameColor set];
        CGContextFillPath(ctx);
        
        // 绘制剪切园
        CGFloat cutX = margin;
        CGFloat cutY = margin;
        CGFloat cutW = imageW;
        CGFloat cutH = imageH;
        CGContextAddEllipseInRect(ctx, CGRectMake(cutX, cutY, cutW, cutH));
        CGContextClip(ctx);
        
        // 绘制裁剪图片
        CGFloat imageX = cutX;
        CGFloat imageY = cutY;
        
        [image drawInRect:CGRectMake(imageX, imageY, imageW, imageH)];
        
        // 获取绘制好的图片
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        
        return newImage;
    
    }
    
    @end
    View Code
  • 相关阅读:
    Cocos Creator之生命周期函数
    Cocos Creator之基本概念
    Cocos Creator之菜单栏和工具栏
    Cocos Creator之认识
    lvs nat模式+iptables实现fullnat
    k8s cpu绑定
    arp代理
    确定veth pair在容器和宿主机的对应关系
    k8s 滚动发布
    http长连接
  • 原文地址:https://www.cnblogs.com/wentianblog/p/3782691.html
Copyright © 2020-2023  润新知