编辑图片的几个方法
第一种
先用UIImage对象加载一张图片
然后转化成CGImageRef放到CGContext中去编辑
第二种 
用CGImageCreate函数创建CGImageRef
然后把CGImageRef放到CGContext中去编辑
第三种
用CGImageCreateCopy 或者 CGImageCreateCopyWithColorSpace
函数拷贝
CGImageRef CGImageCreate (
   size_t width, //图片的宽度
   size_t height, //图片的高度
   size_t bitsPerComponent,
//图片每个颜色的bits,比如rgb颜色空间,有可能是5 或者 8 ==
   size_t bitsPerPixel,
//每一个像素占用的buts,15 位24位 32位等等
   size_t bytesPerRow, //每一行占用多少bytes 注意是bytes不是bits  1byte = 8bit
   CGColorSpaceRef colorspace,
//颜色空间,比如rgb
   CGBitmapInfo bitmapInfo,
//layout ,像素中bit的布局, 是rgba还是 argb,==
   CGDataProviderRef provider,
//数据源提供者,url或者内存==
   const CGFloat decode[],
//一个解码数组
   bool shouldInterpolate,
//抗锯齿参数
   CGColorRenderingIntent intent
//图片渲染相关参数
);
创建 image mask的一个途径
CGImageMaskCreate
给图片加mask有两种方法,
第一种
使用函数 CGImageCreateWithMask 或者 CGImageCreateWithMaskingColors 在图片上直接打上mask
这样做对原来的图片要求带有 alpha通道,假如没有,那么不会有半透明的效果
第二种
使用 CGContextClipToMask 在图形上下问的某个矩形区域打上mask,这样做无论原先的图片有没有alpha通道,都可以实现半透明效果
位图永远是矩形形状的
iphone支持图片格式:
JPEG, GIF, PNG, TIF, ICO, GMP, XBM, and CUR.
创建一张图片,我们需要提供的一些东西
A bitmap data source
An optional Decode Array, 这个数组在渲染的时候会应用在每一个像素上面,是一个颜色变成另外一个颜色。
An interpolation setting, 布尔值,缩放图片的时候是否采用interpolation算法,具体这个算法不怎么了解
渲染意向
图片尺寸
Pixel Format:这个包括3个东西,
1,Bits per component,每一个component有多少Bits
2. Bits per pixel, 每一个像素占多少Bits
3. Bytes per row,每一个占多少Bytes
Color Spaces and Bitmap Layout,这里需要提供的几个东西
1,Whether a bitmap contains an alpha channel. 位图是否包含alpha通道
2。 color components是否已经乘以alpha value
3。数据格式是浮点数还是整型
Bitmap Layout是指color components的指是怎么指定的
下面的参数 bitmapInfo就是 Bitmap Layout
 
CGImageRef CGImageCreate (
   size_t width,
   size_t height,
   size_t bitsPerComponent,
   size_t bitsPerPixel,
   size_t bytesPerRow,
   CGColorSpaceRef colorspace,
   CGBitmapInfo bitmapInfo,
   CGDataProviderRef provider,
   const CGFloat decode[],
   bool shouldInterpolate,
   CGColorRenderingIntent intent
);
选择一个函数来创建图像
CGImageRef CGImageCreate (
   size_t width,
   size_t height,
   size_t bitsPerComponent,
   size_t bitsPerPixel,
   size_t bytesPerRow,
   CGColorSpaceRef colorspace,
   CGBitmapInfo bitmapInfo,
   CGDataProviderRef provider,
   const CGFloat decode[],
   bool shouldInterpolate,
   CGColorRenderingIntent intent
);
这个是比较万能的函数,但是提供的参数也是非常的多
 
CGImageRef CGImageCreateWithJPEGDataProvider (
   CGDataProviderRef source,
   const CGFloat decode[],
   bool shouldInterpolate,
   CGColorRenderingIntent intent
);
从jpeg源中创建图像
CGImageRef CGImageCreateWithPNGDataProvider (
   CGDataProviderRef source,
   const CGFloat decode[],
   bool shouldInterpolate,
   CGColorRenderingIntent intent
);
从png源中创建图像
CGImageRef CGImageCreateWithImageInRect (
   CGImageRef image,
   CGRect rect
);
从一张图片的某块区域创建图片,类似截图
CGImageRef CGImageSourceCreateImageAtIndex (
   CGImageSourceRef isrc,
   size_t index,
   CFDictionaryRef options
);
从一个图片源中创建图片,这个图片源可能包含不止一张图片,0表示第一张
CGImageRef CGImageSourceCreateThumbnailAtIndex (
   CGImageSourceRef isrc,
   size_t index,
   CFDictionaryRef options
);
创建一个缩略图从一个图片源中,这个图片源可能包含不止一张图片,0表示第一张
CGImageRef CGBitmapContextCreateImage (
   CGContextRef c
);
从一个图形上下文中创建图片
 
CGImageRef CGImageCreateCopy (
   CGImageRef image
);
拷贝一张图片
CGImageRef CGImageCreateCopyWithColorSpace (
   CGImageRef image,
   CGColorSpaceRef colorspace
);
拷贝一张图片,替换原来的颜色空间
 
 
 
从jpeg创建图片
void MyCreateAndDrawBitmapImage (CGContextRef myContext, 
                                CGRect myContextRect,
                                const char *filename);
{
    CGImageRef image;
    CGDataProviderRef provider;
    CFStringRef path;
    CFURLRef url;
 
    path = CFStringCreateWithCString (NULL, filename,
                        kCFStringEncodingUTF8);
    url = CFURLCreateWithFileSystemPath (NULL, path, // 2
                            kCFURLPOSIXPathStyle, NO);
    CFRelease(path);
    provider = CGDataProviderCreateWithURL (url);// 3
    CFRelease (url);
    image = CGImageCreateWithJPEGDataProvider (provider,// 4
                                    NULL,
                                    true,
                                    kCGRenderingIntentDefault);
    CGDataProviderRelease (provider);// 5
    CGContextDrawImage (myContext, myContextRect, image);// 6
    CGImageRelease (image);// 7
}
CGImageRef CGImageMaskCreate (
        size_t width,
        size_t height,
        size_t bitsPerComponent,
        size_t bitsPerPixel,
        size_t bytesPerRow,
        CGDataProviderRef provider,
        const float decode[],
        int shouldInterpolate
);
从一个已经存在的图形上下文中创建一个CGLayerRef
CGLayerRef CGLayerCreateWithContext (
   CGContextRef context,
   CGSize size,
   CFDictionaryRef auxiliaryInfo
);
CGSize CGLayerGetSize (
   CGLayerRef layer
);
然后从CGLayerRef中得到CGContextRef,就是图形上下文
CGContextRef CGLayerGetContext (
   CGLayerRef layer
);
接着在这个CGContextRef中绘画
Draw the CGLayer to the Destination Graphics Context
void CGContextDrawLayerInRect (
   CGContextRef context,
   CGRect rect,
   CGLayerRef layer
);
void CGContextDrawLayerAtPoint (
   CGContextRef context,
   CGPoint point,
   CGLayerRef layer
);

版权声明:本文为博主原创文章,未经博主允许不得转载。

IOS-图片操作集合的更多相关文章

  1. 实现iOS图片等资源文件的热更新化(三):动态的资源文件夹

    简介 此文,将尝试动态从某个不确定的文件夹中加载资源文件.文章,会继续完善自定义的 imageNamed 函数,并为下一篇文章铺垫. 这么做的意义 正如我们经常所说的那样,大多数情景知道做事的意义往往 ...

  2. 实现iOS图片等资源文件的热更新化(五): 一个简单完整的资源热更新页面

    简介 一个简单的关于页面,有一个图片,版本号,App名称等,着重演示各个系列的文章完整集成示例. 动机与意义 这是系列文章的最后一篇.今天抽空写下,收下尾.文章本身会在第四篇的基础上,简单扩充下代码, ...

  3. 实现iOS图片等资源文件的热更新化(零): 序

    必要的序 以后在写系列文章,准备把基本的规划和动机等,单独作为一个小的序言部分给独立出来.序言部分,可以较为完整地交待系列文章的写作动机,所展示的编码技术可能的应用场景等.个人,我还是比较看重文章或者 ...

  4. 大屏iPhone的适配 +iOS 图片尺寸要求

    摘自:http://blog.ibireme.com/2014/09/16/adapted_to_iphone6/ 苹果公司官网设计介绍到:Retina显示屏的超高像素密度已超过人眼能分辨的范围.Re ...

  5. js 前端图片压缩+ios图片角度旋转

    step1:读取选择的图片,并转为base64: function ImgToBase64 (e, fn) { // 图片方向角 //fn为传入的方法函数,在图片操作完成之后执行 var Orient ...

  6. C#——图片操作类简单封装

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...

  7. iOS 线程操作库 PromiseKit

    iOS 线程操作库 PromiseKit 官网:http://promisekit.org/ github:https://github.com/mxcl/PromiseKit/tree/master ...

  8. iOS 图片的解压缩

    一.图片加载的工作流 概括来说,从磁盘中加载一张图片,并将它显示到屏幕上,中间的主要工作流如下: 假设我们使用 +imageWithContentsOfFile: 方法从磁盘中加载一张图片,此时的图片 ...

  9. iOS 图片加载速度优化

    FastImageCache 是 Path 团队开发的一个开源库,用于提升图片的加载和渲染速度,让基于图片的列表滑动起来更顺畅,来看看它是怎么做的. 一.优化点 iOS 从磁盘加载一张图片,使用 UI ...

  10. ECharts外部调用保存为图片操作及工作流接线mouseenter和mouseleave由于鼠标移动速度过快导致问题解决办法

    记录两个项目开发中遇到的问题,一个是ECharts外部调用保存为图片操作,一个是workflow工作流连接曲线onmouseenter和onmouseleave事件由于鼠标移动过快触发问题. 一.外部 ...

随机推荐

  1. ajax联动

    1.编写html代码,引入ajax文件 <script type="text/javascript" src="js/ajax.js"></s ...

  2. java 在方法中新建线程,传参和加锁详解

    在实际开发中,往往在基本两三种创建线程的方法之外,还用到一个简单的创建线程调用方法的情况,代码如下: public void deleteRedisData(RedisKey redisKey){ n ...

  3. c语言中的字符数组与字符串

    1.字符数组的定义与初始化 字符数组的初始化,最容易理解的方式就是逐个字符赋给数组中各元素. char str[10]={ 'I',' ','a','m',' ',‘h’,'a','p','p','y ...

  4. UVa1368/ZOJ3132 DNA Consensus String

    #include <stdio.h>#include <string.h> int main(){    int a[4][1000]; // A/C/G/T在每列中出现的次数 ...

  5. MysqlHelp

    using System.Configuration;using MySql.Data: public class MySqlHelp { //链接字符串 private static string ...

  6. C# 自定义控件的一些文章和博客

    http://blog.csdn.net/songkexin/archive/2009/12/08/4961215.aspx http://www.cnblogs.com/yuanfan/archiv ...

  7. poj 3185 The Water Bowls 高斯消元枚举变元

    题目链接 给一行0 1 的数, 翻转一个就会使他以及它左右两边的都变, 求最少多少次可以变成全0. 模板题. #include <iostream> #include <vector ...

  8. [LeetCode]题解(python):002-Add Two Numbers

    题目来源: https://leetcode.com/problems/add-two-numbers/ 题意分析: 这道题目是要将两个单链条相加.输出得到的新链条. 题目思路: 不难发现,其实题目就 ...

  9. (Problem 73)Counting fractions in a range

    Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...

  10. haproxy 超时自动重发

    timeout connect 5000 timeout client 50000 timeout server 50000 timeout check 5s stats refresh 30s Ap ...