2012-06-27 79 views

回答

10

您可以使用此代碼:

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize { 
    //UIGraphicsBeginImageContext(newSize); 
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0); 
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();  
    UIGraphicsEndImageContext(); 
    return newImage; 
} 

here服用。

額外的工作,你都會有,將是一個方法的將調用imageWithImage:scaledToSize:UISlider's值更改時。

+0

THX的回覆,我已經與後續添加的,對嗎​​?但我測試有麻煩圖像大小發生... 浮點率= penSize_sld.value /(penSize_sld.maximumValue/2); CGSize ss = CGSizeMake(penSize_sld.currentThumbImage.size.width * ratio,penSize_sld.currentThumbImage.size.height * ratio); UIImage * changeImage = [UIImage imageWithImage:penSize_sld.currentThumbImage scaledToSize:ss]; [penSize_sld setThumbImage:changeImage forState:UIControlStateNormal];如果(發送者== penSize_sld){brush_width = penSize_sld.value; } – AndyYeung

+1

如果您需要添加額外的信息,編輯您的帖子,並把這些代碼有... – Peres

+0

的工作!感謝JackyBoy '浮子比= penSize_sld.value /(penSize_sld.maximumValue/2); 如果(比<0.8){ 比= 0.8; UIImage * thumbImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@「drawview_dragbar_bu_1.png」ofType:nil]];}};}}; CGSize SS = CGSizeMake(thumbImage.size.width *比,thumbImage.size.height *比); UIImage * changeImage = [UIImage imageWithImage:thumbImage scaledToSize:ss]; [penSize_sld setThumbImage:changeImage forState:UIControlStateNormal]; ..... }' – AndyYeung

2

斯威夫特3:

extension UIImage { 

    func scaleToSize(newSize: CGSize) -> UIImage { 
     UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0) 
     draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)) 
     let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()! 
     UIGraphicsEndImageContext(); 
     return newImage 
    } 
}