2013-03-15 10 views
7

क्या कोई तरीका है कि यूआईएममेज को यथासंभव कम से कम लाइनों में कैसे बदला जाए? मुझे अनुपात का कोई फर्क नहीं पड़ता, मैं सिर्फ छवि संकल्प को 80x60 पर सेट करना चाहता हूं। यह सबXCODE में UIImage का सरल आकार बदलने

उत्तर

13

यह अधिक हो सकता है लेकिन, आप बस अपनी छवि ले सकते हैं, और उस संकल्प पर एक ग्राफिक्स संदर्भ बना सकते हैं, फिर आप tempImage को UIImageView के रूप में सेट कर सकते हैं, इसे ओवरराइट कर सकते हैं।

 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

क्यों ओवरकिल ??? मेरा मतलब है, यह काम करता है, लेकिन क्या इसका कोई नकारात्मक प्रभाव पड़ता है? – user1602687

+0

आपने कहा था कि आप कुछ लाइनें चाहते थे। यह 10 की तरह है, लॉल। हालांकि कोई नकारात्मक प्रभाव नहीं! – ApolloSoftware

+0

मैंने कोशिश की लेकिन यह धुंधला हो गया? – Lion789

1

इस वर्ग का उपयोग करें (तदनुसार फ़ाइल ज पर कोड शामिल)

#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 जोड़ने के लिए मत भूलना।

0

मैं 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; 
} 

आपको बस इतना करना है i_width के साथ प्रदान करना है, और तदनुसार यह स्केल करेगा।

मैंने इसमें थोड़ा मोड़ जोड़ा है, ताकि अगर चित्र लैंडस्केप मोड में है, तो इसे पोर्ट्रेट पर घुमाया जाएगा, और फिर आकार बदल जाएगा। आप यह विपरीत (परिदृश्य को चित्र) होने के लिए चाहते हैं, यह परिवर्तन:

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

चेतावनी: यदि चित्र इशारा कर रही है मेरी घूर्णन विधि को ध्यान में नहीं ले करता है बायें या दायें। दूसरे शब्दों में, यदि कोई छवि ऊपर की तरफ से परिदृश्य है, तो मेरा कोड उसे पहचान नहीं सकता है। मुझे आशा है कि कोई और इस पर कुछ प्रकाश डाल सकता है :)

संबंधित मुद्दे