首页  >  文章  >  后端开发  >  iOS开发百问(4)

iOS开发百问(4)

黄舟
黄舟原创
2017-01-20 09:29:321073浏览

32、UIImage+Scale缩放图片
UIImage可以加载图片,但是我们想要得到一张缩小或放大的图片,利用UIImage不能做到,下面我们添加一个UIImage的分类,用来实现UIImage中图片的放大和缩小。
首先,创建一个UIImage+Scale类。
然后,实现该类的方法:

#import <UIKit/UIKit.h>
@interface UIImage (scale)
-(UIImage*)scaleToSize:(CGSize)size;
@end
#import "UIImage+Scale.h"
@implementation UIImage (scale)
-(UIImage*)scaleToSize:(CGSize)size
{
// 创建一个bitmap的context
// 并把它设置成为当前正在使用的context
UIGraphicsBeginImageContext(size);
// 绘制改变大小的图片
[self drawInRect:CGRectMake(0, 0, size.width,size.height)];
// 从当前context中创建一个改变大小后的图片
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// 使当前的context出堆栈
UIGraphicsEndImageContext();
// 返回新的改变大小后的图片
return scaledImage;
}
@end

最后,就是该类的使用了:

#import "UIImage+Scale.h"
[[UIImage imageNamed:”p.png”] scaleToSize:CGSizeMake(252.0f,192.0f)];

33、Coreplot:在散点图中,legendTitleForBarPlot不会被调用
legendTitleForBarPlot是柱状图的数据源方法,在散点图的数据源委托CPTScatterPlotDataSource 中没有该方法。要定制 legend 的标题,唯一的方法是指定plot 的 title 属性。如果 title 为空,则使用 identifier 属性。

34、 setHidesBackButton不能隐藏返回按钮
将setHidesBackButton:animated:移到 viewDidAppear: 方法,而不要在 viewWillAppear:或者viewDidLoad方法中。

35、cannotfind protocol declaration NSURLConnectionDelegate
iOS5开始NSURLConnectionDelegate被deprecated,在NSURLConnection.h中,这些方法变成了非正式协议。同时复制了一份这些方法的拷贝到正式协议NSURLConnectionDataDelegate中。你可以直接将类接口声明的6c7586e62da791676357131c39ce00fc删除,并实现这些方法,从而使用非正式协议。
36、警告“Property'ssynthesized getter follows Cocoa naming convention for returning 'owned'objects”
要synthesized的属性中,属性名不得以“new”开头,比如“newFeature”。
37、 Implicit declaration of function 'xxx' is invalidin C99
这是Xcode的一个bug。当编译器第一次看见函数定义,却未找到该函数原型时会报此错误。解决方法是在函数定义之前加入函数原型声明。注意,把函数原型声明语句插入到类的interface声明内(.h头文件),或者的类implementation语句之前(.m文件)。
38、-[UIImageresizableImageWithCapInsets:]: unrecognized selector
这个方法是iOS5中新增的,在iOS4中请使用stretchableImageWithLeftCapWidth:topCapHeight:方法。代码:

if([img respondsToSelector:@selector(resizableImageWithCapInsets:)])
{//for iOS 5+
img=[srcImg resizableImageWithCapInsets:UIEdgeInsetsMake(0, 6, 0, 6)]; 
}else{//iOS 4 compatibility
img=[srcImg stretchableImageWithLeftCapWidth:6 topCapHeight:0];
}

39、计算指定字体的字符串Size

CGSizemaximumLabelSize = CGSizeMake(250 ,MAXFLOAT);
CGSizeexpectedLabelSize = [LABEL.text sizeWithFont:[UIFontsystemFontOfSize:UILabel.font]
constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeWordWrap];

expectedLabelSize即根据字体、最大size限制、换行模式计算出来的实际Size。
40、ASIHTTPRequestclearDelegateAndCancel方法导致程序崩溃
ASIHTTPRequest并不会持有delegate对象,当你取消一个request或delegate释放后,为了避免调用释放了的delegate方法,我们应当取消request。但是clearDelegateAndCancel方法会导致一个调用deallocated对象错误并崩溃。
为了避免这个,你应当(针对1.8.1及之前的版本):
在delegate中持有ASIHTTPRequest对象;
当释放delegate或取消request时,使用不要调用clearDelegateAndCancel而改用“[requestrelease],request=nil;”。
41、 Castof 'int' to 'CAMediaTimingFunction *' is disallowed with ARC
以下代码导致上述错误:

transition.timingFunction= UIViewAnimationCurveEaseInOut;

事实上,就算在MRC(手动内存管理)中,这句代码也是不正确的。之所以能够不出错,是因为UIViewAnimationCurveEaseInOut通常为0,转换过来就变成了nil。实际上这句代码应该修改为:

[animationsetTimingFunction:[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut]];


以上就是iOS开发百问(4)的内容,更多相关内容请关注PHP中文网(www.php.cn)!


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn