




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、iPhone UIImage扩展方法,支持放大和旋转#import <Foundation/Foundation.h>interface UIImage (CS_Extensions)- (UIImage *)imageAtRect:(CGRect)rect;- (UIImage *)imageByScalingProportionallyToMinimumSize:(CGSize)targetSize;- (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize;- (UIImage *)imageBySca
2、lingToSize:(CGSize)targetSize;- (UIImage *)imageRotatedByRadians:(CGFloat)radians;- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees;end; / UIImage-Extensions.m/ Created by Hardy Macia on 7/1/09./ Copyright 2009 Catamount Software. All rights reserved./#import "UIImage-Extensions.h"CGFloa
3、t DegreesToRadians(CGFloat degrees) return degrees * M_PI / 180;CGFloat RadiansToDegrees(CGFloat radians) return radians * 180/M_PI;implementation UIImage (CS_Extensions)-(UIImage *)imageAtRect:(CGRect)rectCGImageRef imageRef = CGImageCreateWithImageInRect(self CGImage, rect);UIImage* subImage = UII
4、mage imageWithCGImage: imageRef;CGImageRelease(imageRef);return subImage;- (UIImage *)imageByScalingProportionallyToMinimumSize:(CGSize)targetSize UIImage *sourceImage = self;UIImage *newImage = nil;CGSize imageSize = sourceImage.size;CGFloat width = imageSize.width;CGFloat height = imageSize.height
5、;CGFloat targetWidth = targetSize.width;CGFloat targetHeight = targetSize.height;CGFloat scaleFactor = 0.0;CGFloat scaledWidth = targetWidth;CGFloat scaledHeight = targetHeight;CGPoint thumbnailPoint = CGPointMake(0.0,0.0);if (CGSizeEqualToSize(imageSize, targetSize) = NO) CGFloat widthFactor = targ
6、etWidth / width;CGFloat heightFactor = targetHeight / height;if (widthFactor > heightFactor)scaleFactor = widthFactor;elsescaleFactor = heightFactor;scaledWidth = width * scaleFactor;scaledHeight = height * scaleFactor;/ center the imageif (widthFactor > heightFactor) thumbnailPoint.y = (targe
7、tHeight - scaledHeight) * 0.5; else if (widthFactor < heightFactor) thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;/ this is actually the interesting part:UIGraphicsBeginImageContext(targetSize);CGRect thumbnailRect = CGRectZero;thumbnailRect.origin = thumbnailPoint;thumbnailRect.size.width
8、 = scaledWidth;thumbnailRect.size.height = scaledHeight;sourceImage drawInRect:thumbnailRect;newImage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();if(newImage = nil) NSLog("could not scale image");return newImage ;- (UIImage *)imageByScalingProportionallyToSize
9、:(CGSize)targetSize UIImage *sourceImage = self;UIImage *newImage = nil;CGSize imageSize = sourceImage.size;CGFloat width = imageSize.width;CGFloat height = imageSize.height;CGFloat targetWidth = targetSize.width;CGFloat targetHeight = targetSize.height;CGFloat scaleFactor = 0.0;CGFloat scaledWidth
10、= targetWidth;CGFloat scaledHeight = targetHeight;CGPoint thumbnailPoint = CGPointMake(0.0,0.0);if (CGSizeEqualToSize(imageSize, targetSize) = NO) CGFloat widthFactor = targetWidth / width;CGFloat heightFactor = targetHeight / height;if (widthFactor < heightFactor)scaleFactor = widthFactor;elsesc
11、aleFactor = heightFactor;scaledWidth = width * scaleFactor;scaledHeight = height * scaleFactor;/ center the imageif (widthFactor < heightFactor) thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; else if (widthFactor > heightFactor) thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;/ t
12、his is actually the interesting part:UIGraphicsBeginImageContext(targetSize);CGRect thumbnailRect = CGRectZero;thumbnailRect.origin = thumbnailPoint;thumbnailRect.size.width = scaledWidth;thumbnailRect.size.height = scaledHeight;sourceImage drawInRect:thumbnailRect;newImage = UIGraphicsGetImageFromC
13、urrentImageContext();UIGraphicsEndImageContext();if(newImage = nil) NSLog("could not scale image");return newImage ;- (UIImage *)imageByScalingToSize:(CGSize)targetSize UIImage *sourceImage = self;UIImage *newImage = nil;/ CGSize imageSize = sourceImage.size;/ CGFloat width = imageSize.wid
14、th;/ CGFloat height = imageSize.height;CGFloat targetWidth = targetSize.width;CGFloat targetHeight = targetSize.height;/ CGFloat scaleFactor = 0.0;CGFloat scaledWidth = targetWidth;CGFloat scaledHeight = targetHeight;CGPoint thumbnailPoint = CGPointMake(0.0,0.0);/ this is actually the interesting pa
15、rt:UIGraphicsBeginImageContext(targetSize);CGRect thumbnailRect = CGRectZero;thumbnailRect.origin = thumbnailPoint;thumbnailRect.size.width = scaledWidth;thumbnailRect.size.height = scaledHeight;sourceImage drawInRect:thumbnailRect;newImage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEnd
16、ImageContext();if(newImage = nil) NSLog("could not scale image");return newImage ;- (UIImage *)imageRotatedByRadians:(CGFloat)radiansreturn self imageRotatedByDegrees:RadiansToDegrees(radians);- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees / calculate the size of the rotated view'
17、s containing box for our drawing spaceUIView *rotatedViewBox = UIView alloc initWithFrame:CGRectMake(0,0,self.size.width, self.size.height);CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees);rotatedViewBox.transform = t;CGSize rotatedSize = rotatedViewBox.frame.size;rotate
18、dViewBox release;/ Create the bitmap contextUIGraphicsBeginImageContext(rotatedSize);CGContextRef bitmap = UIGraphicsGetCurrentContext();/ Move the origin to the middle of the image so we will rotate and scale around the center.CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);/ / Rotate the image contextCGContextRotateCTM(bitmap, DegreesToRadians(degrees);/ Now, draw the rotated/scaled image into the
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 【正版授权】 ISO/IEC 23090-7:2022/AMD1:2024 EN Information technology - Coded representation of immersive media - Part 7: Immersive media metadata - Amendment 1: Common metadata for imme
- 【正版授权】 ISO 7718-2:2025 EN Aircraft - Passenger doors interface requirements for connection of passenger boarding bridge or passenger transfer vehicle - Part 2: Upper deck doors
- 【正版授权】 ISO 16900-11:2025 EN Respiratory protective devices - Methods of test and test equipment - Part 11: Determination of field of vision
- 【正版授权】 IEC 81355-1:2024 EN/FR Industrial systems,installations and equipment and industrial products - Classification and designation of information - Part 1: Basic rules and clas
- 2025年集成电路代工厂商业秘密保护合同
- 2025年动力调谐陀螺平台项目建议书
- 提升学校艺术教育影响力的策略计划
- 医院宣传工作总结与改进方法计划
- 班级传统文化传承活动的计划
- 营养科膳食服务效果评估与改进计划
- 一次性纤维环缝合器
- 中华民族的形成与发展
- 2023年上海中侨职业技术大学单招考试职业技能考试模拟试题及答案解析
- 儿科抗生素使用
- 中国教育公益领域发展报告
- 第2章第1节有机化学反应类型课件高二下学期化学鲁科版选择性必修3
- 绿化工程承包合同 绿化工程承包合同范本(二篇)
- 建筑财务出纳年终总结PPT模板下载
- GB/T 9574-2001橡胶和塑料软管及软管组合件试验压力、爆破压力与设计工作压力的比率
- 三位数乘一位数练习题(300道)
- 校本课程《竹之匠艺》
评论
0/150
提交评论