码迷,mamicode.com
首页 > 其他好文 > 详细

Quartz2D练习 -- 裁剪图片分类

时间:2014-06-12 07:25:28      阅读:343      评论:0      收藏:0      [点我收藏+]

标签:des   style   class   blog   code   java   

Main.storyboard

bubuko.com,布布扣
bubuko.com,布布扣
<?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
bubuko.com,布布扣

LWTViewController.h

bubuko.com,布布扣
bubuko.com,布布扣
#import <UIKit/UIKit.h>

@interface LWTViewController : UIViewController

@end
View Code
bubuko.com,布布扣

LWTViewController.m

bubuko.com,布布扣
bubuko.com,布布扣
 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
bubuko.com,布布扣

UIImage+cutImageRound.h

bubuko.com,布布扣
bubuko.com,布布扣
#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
bubuko.com,布布扣

UIImage+cutImageRound.m

bubuko.com,布布扣
bubuko.com,布布扣
//
//  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
bubuko.com,布布扣

Quartz2D练习 -- 裁剪图片分类,布布扣,bubuko.com

Quartz2D练习 -- 裁剪图片分类

标签:des   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/wentianblog/p/3782691.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!