2012-06-27 10 views

risposta

10

È possibile utilizzare questo codice:

+ (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; 
} 

Tratto da here.

Il lavoro in più si avrà, sarà un metodo A che chiamerà i imageWithImage:scaledToSize: quando il valore cambia UISlider's.

+0

thx per la risposta, ho aggiunto con il seguito, ho ragione? Ma provo con problemi con le dimensioni dell'immagine ... rapporto float = 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 ridimensionatoToSize: ss]; [penSize_sld setThumbImage: changeImage forState: UIControlStateNormal]; if (mittente == penSize_sld) { brushWidth = penSize_sld.value; } – AndyYeung

+1

Se hai bisogno di aggiungere ulteriori informazioni, modifica il tuo post e metti il ​​codice lì ... – Peres

+0

Questo lavoro! Grazie JackyBoy 'float ratio = penSize_sld.value/(penSize_sld.maximumValue/2); if (rapporto <0,8) { rapporto = 0,8; } UIImage * thumbImage = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "drawview_dragbar_bu_1.png" ofType: nil]]; CGSize ss = CGSizeMake (thumbImage.size.width * ratio, thumbImage.size.height * ratio); UIImage * changeImage = [UIImage imageWithImage: thumbImage scaledToSize: ss]; [penSize_sld setThumbImage: changeImage forState: UIControlStateNormal]; ..... } ' – AndyYeung

2

Swift 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 
    } 
} 
Problemi correlati