XCODE

2013-03-15 34 views
7

'da UIImage'ın yeniden boyutlandırılması UIImage'ın mümkün olduğunca az satırda nasıl yeniden boyutlandırılacağı var mı? Oranın farkında değil, sadece görüntü çözünürlüğünü 80x60 olarak ayarlamak istiyorum. Bu tümXCODE

cevap

13

Bu aşırı bir şekilde olabilir, ancak görüntünüzü alabilir ve istediğiniz çözünürlükte bir grafik bağlamı oluşturabilir, ardından tempImage'ı UIImageView olarak ayarlayabilir ve üzerine yazabilirsiniz.

 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

neden overkill ??? Demek istediğim, çalışıyor, ama olumsuz bir etkisi var mı? – user1602687

+0

Birkaç satır istediğini söyledin. Bu 10 gibi, lol. Olsa da olumsuz bir etki yok! – ApolloSoftware

+0

Bunu denedim ama bulanıklaşıyor mu? – Lion789

1

kullanın bu sınıf (buna göre dosyayı .h kodu ekleyin)

#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(); 
} 

CoreGraphics frameawork eklemeyi unutmayın.

0

Ben this page üzerine aşağıdaki kodu buldum:

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

yapmanız gereken tek şey i_width ile bunu sağlamak olduğunu ve buna göre ölçeklenecektir.

Resimde manzara modundaysa, dikey olarak döndürülüp yeniden boyutlandırılacak şekilde küçük bir bükülme ekledim. Eğer tam tersi (peyzaja dikey) olmasını istiyorsanız, bunu değiştirmek: Buna

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

:

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

Ikaz: resim işaret eğer benim dönen yöntem dikkate almaz sol veya sağ. Başka bir deyişle, bir resim baş aşağı yatay ise, kodum bunu tanıyamaz. Umarım başka biri bu konuda biraz ışık tutabilir :)