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

转载于:https://www.cnblogs.com/wentianblog/p/3782691.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值