2013-06-10 16 views
19

ciao Voglio inviare un'immagine a un servizio web. Ma voglio ridimensionare l'utente selezionato qualsiasi immagine in 75 x 75 e inviarlo al servizio web. Come posso ridimensionare questa immagine in 75 x 75Come ridimensionare un'immagine in iOS?

Grazie

+0

http://stackoverflow.com/questions/10491080/uiimage-resizing-not-working-properly/10491692 # 10491692 controlla questa fantastica risposta di Rob – pmk

risposta

38

cercare di attuare il codice qui sotto. Può essere utile per voi:

CGRect rect = CGRectMake(0,0,75,75); 
    UIGraphicsBeginImageContext(rect.size); 
    [yourCurrentOriginalImage drawInRect:rect]; 
    UIImage *picture1 = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    NSData *imageData = UIImagePNGRepresentation(picture1); 
    UIImage *img=[UIImage imageWithData:imageData]; 
+0

ciao grazie per la risposta, Se invio in formato JPG, come cambiare questo 'UIImagePNGRepresentation' – iDia

+0

puoi utilizzare l'interfaccia utente ImageJPEGRappresentazione, ha bisogno della qualità in float. – ilmetu

+0

Perché è stato aggiunto il passaggio aggiuntivo in cui si creano dati PNG solo per creare nuovamente una nuova immagine? – rckoenes

9

provare questo codice:

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

Utilizzare questo metodo per ridimensionare l'immagine:

+(UIImage *)imageResize :(UIImage*)img andResizeTo:(CGSize)newSize 
{ 
    CGFloat scale = [[UIScreen mainScreen]scale]; 
    /*You can remove the below comment if you dont want to scale the image in retina device .Dont forget to comment UIGraphicsBeginImageContextWithOptions*/ 
    //UIGraphicsBeginImageContext(newSize); 
    UIGraphicsBeginImageContextWithOptions(newSize, NO, scale); 
    [img drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; 
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 
} 
Problemi correlati