iOS 类别

本文将介绍Objective-C语言的新物性,类别(categories)允许在现有的类中添加用户自己的方法,有时我们需要在一个已经定义好的类中增加一些方法,而不想去重写该类。比如,当工程已经很大,代码量比较多,或者类中已经包住很多方法,已经有其他代码调用了该类创建对象并使用该类的方法时,可以使用类别对该类扩充新的方法。

  注意:1.类别只能扩充方法,而不能扩充成员变量。

    2.名称冲突,即类别中的方法与现有方法重名。当发生名称冲突时,类别具有更高的优先级。可以在自己的类别方法名中增加一个前缀,以确保不发生名称冲突。

  实例分析:

  1、目的:在我的工程中,我需要对图片进行压缩,此时我想到类别,利用类别对UIImage类进行扩展,增加图片压缩方法。

  2、类别定义:

类别.h声明文件

 

#import <Foundation/Foundation.h>

 

@interface UIImage (UIImageExt)

//这个方法就是我添加的图片压缩的方法

- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize;

@end

 

 

类别.m实现文件

 

 

#import "UIImageExt.h"

 

@implementation UIImage (UIImageExt)

 

- (UIImage*)imageByScalingAndCroppingForSize:(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 = 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; // scale to fit height

else

scaleFactor = heightFactor; // scale to fit width

scaledWidth= width * scaleFactor;

scaledHeight = height * scaleFactor;

 

// center the image

if (widthFactor > heightFactor)

{

thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;

}

else if (widthFactor < heightFactor)

{

thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;

}

}

 

UIGraphicsBeginImageContext(targetSize); // this will crop

 

CGRect thumbnailRect = CGRectZero;

thumbnailRect.origin = thumbnailPoint;

thumbnailRect.size.width= scaledWidth;

thumbnailRect.size.height = scaledHeight;

 

[sourceImage drawInRect:thumbnailRect];

 

newImage = UIGraphicsGetImageFromCurrentImageContext();

if(newImage == nil)

NSLog(@"could not scale image");

 

//pop the context to get back to the default

UIGraphicsEndImageContext();

return newImage;

}

 

@end

   3、如何使用类别
   在上文我已经对UIImage进行了扩展,下面如何将演示在我的工程中如何调用该方法:

//根据图片tag显示图片

-(void)showPhotoBySerialNumber:(int)imageTag;

{

//这个largeImageArray是NSMutableArray类型的,存放图片存储路径,根据路径得到UIImage

UIImage *img = [UIImage imageWithContentsOfFile:[self.largeImageArray objectAtIndex:imageTag]];

//MyScrollView是我自定义的ScrollView,目的是使ScrollView响应点击事件,关于如何自定义的ScrollView在以后的博客中,我将会阐述

MyScrollView *scrView = [[MyScrollView alloc] initWithFrame:CGRectMake(340*imageTag, 0, 320, 480)];

scrView.host self;

//这句就是调用了类别,通过UIImage实例对象,调用imageByScalingAndCroppingForSize:类别

scrView.image = [img imageByScalingAndCroppingForSize:CGSizeMake(320.0, 480.0)];

scrView.tag = imageTag+100;

//下面这句,就是把上面的scrView塞到imageScrollView上,imageScrollView是UIScrollView类型

[self.imageScrollView addSubview:scrView];

[scrView release];

 

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值