iOS開發百問(4)

來源:互聯網
上載者:User

標籤:des   style   code   http   ext   color   

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中。你可以直接將類介面聲明的<NSURLConnectionDelegate>刪除,並實現這些方法,從而使用非正式協議。

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]];

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.