2013-03-15 13 views

risposta

13

Questo può essere eccessivo, ma puoi semplicemente prendere la tua immagine e creare un contesto grafico a quella risoluzione che desideri, quindi puoi impostare tempImage come UIImageView, sovrascrivendolo.

 UIImage *image = YourImageView.image; 
     UIImage *tempImage = nil; 
     CGSize targetSize = CGSizeMake(80,60); 
     UIGraphicsBeginImageContext(targetSize); 

     CGRect thumbnailRect = CGRectMake(0, 0, 0, 0); 
     thumbnailRect.origin = CGPointMake(0.0,0.0); 
     thumbnailRect.size.width = targetSize.width; 
     thumbnailRect.size.height = targetSize.height; 

     [image drawInRect:thumbnailRect]; 

     tempImage = UIGraphicsGetImageFromCurrentImageContext(); 

     UIGraphicsEndImageContext(); 

     YourImageView.image = tempImage; 
+0

perché overkill ??? Voglio dire, funziona, ma ha qualche effetto negativo? – user1602687

+0

hai detto che volevi poche righe. questo è come 10, lol. Nessun effetto negativo però! – ApolloSoftware

+0

Ho provato questo ma finisce sfocato? – Lion789

1

utilizzare questa classe (aggiungere il codice per .h file di conseguenza)

#import "UIImage+Resize.h" 

@implementation UIImage (Resize) 

- (UIImage *)resizedImage:(CGSize)bounds { 
    return [self resizedImage:bounds upScale:YES]; 
} 

- (UIImage*)resizedImage:(CGSize)bounds upScale:(BOOL)upScale { 
    CGSize originalSize = self.size; 
    float xScale = bounds.width/originalSize.width; 
    float yScale = bounds.height/originalSize.height; 
    float scale = MIN(xScale, yScale); 

    if (!upScale) { 
     scale = MIN(scale, 1); 
    } 

    CGSize newSize = CGSizeMake(originalSize.width * scale, originalSize.height * scale); 
    UIGraphicsBeginImageContextWithOptions(newSize, NO, [UIScreen mainScreen].scale); 
    [self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; 
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return resultImage; 
} 
1
- (void)resizeImage:(UIImage *)image 
{ 
    CGSize origImageSize = [image size]; 
    CGRect imageRect = CGRectMake(0, 0, 80, 60); 
    float ratio = MAX(newRect.size.width/origImageSize.width, 
        newRect.size.height/origImageSize.height); 
    UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0); 
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:newRect 
               cornerRadius:5.0]; 
    [path addClip]; 
    CGRect imageRect; 
    imageRect.size.width = ratio * origImageSize.width; 
    imageRect.size.height = ratio * origImageSize.height; 
    imageRect.origin.x = (newRect.size.width - imageRect.size.width)/2.0; 
    imageRect.origin.y = (newRect.size.height - imageRect.size.height)/2.0; 
    [image drawInRect:imageRect]; 
    UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext(); 
    NSData *data = UIImagePNGRepresentation(smallImage); 
    UIGraphicsEndImageContext(); 
} 

Non dimenticare di aggiungere CoreGraphics frameawork.

0

ho trovato il seguente codice sul this page:

- (UIImage*)imageWithImage: (UIImage*) sourceImage scaledToWidth: (float) i_width{ 

if (sourceImage.size.width>sourceImage.size.height) { 
    sourceImage = [[UIImage alloc] initWithCGImage: sourceImage.CGImage 
              scale: 1.0 
             orientation: UIImageOrientationRight]; 
} 

float oldWidth = sourceImage.size.width; 
float scaleFactor = i_width/oldWidth; 
float newHeight = sourceImage.size.height * scaleFactor; 
float newWidth = oldWidth * scaleFactor; 

UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight)); [sourceImage drawInRect:CGRectMake(0, 0, newWidth, newHeight)]; 
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 
return newImage; 
} 

Tutto quello che dovete fare è fornire con la i_width, e scalerà di conseguenza.

Ho aggiunto un piccolo tocco ad esso, in modo che se l'immagine è in modalità orizzontale, verrà ruotata in verticale e quindi ridimensionata. Se si vuole che sia il contrario (verticale a orizzontale), modificare questa:

if (sourceImage.size.width>sourceImage.size.height) { 
    sourceImage = [[UIImage alloc] initWithCGImage: sourceImage.CGImage 
              scale: 1.0 
             orientation: UIImageOrientationRight]; 
} 

a questo:

if (sourceImage.size.height>sourceImage.size.width) { 
    sourceImage = [[UIImage alloc] initWithCGImage: sourceImage.CGImage 
              scale: 1.0 
             orientation: UIImageOrientationRight]; 
} 

avvertimento: il mio metodo di rotazione non prende in considerazione se l'immagine sia rivolta sinistra o destra. In altre parole, se un'immagine è orizzontale capovolta, il mio codice non può riconoscerla. Spero che qualcun altro possa far luce su questo però :)

Problemi correlati