2014年8月22日 星期五

Compress Image programmatically using Swift and Object-C language

【說明】

此份筆記將紀錄如何使用畫布來改變Image的大小。


【片段程式碼】

<Swift>
UIGraphicsBeginImageContextWithOptions(CGSizeMake(48.0, 48.0), false, UIScreen.mainScreen().scale)
image.drawInRect(CGRectMake(0, 0, 48, 48))
var smallImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

<Object-C>
UIGraphicsBeginImageContextWithOptions(CGSizeMake(48.0, 48.0), NO, [UIScreen mainScreen].scale);[image drawInRect:CGRectMake(0, 0, 48.0, 48.0)];
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
建立48*48的畫布,不透明度設否,比例使用偵測的方式調整。將圖片畫至畫布內。取得畫布的圖片。釋放畫布。


【執行結果】

沒有留言:

張貼留言