2009-08-11 7 views
33

मुझे कैमरे से UIimages मिल रहा है और उन्हें प्रदर्शित करने के लिए UIImageViews को असाइन किया जा रहा है। जब मैं ऐसा करता हूं तो कैमरा मुझे 1200 x 1600 पिक्सेल छवि देता है जिसे मैं अपने आवेदन में UIImageView को असाइन करता हूं। छवि को इस स्थिति के तहत छवि दृश्य में अपेक्षित के रूप में प्रदर्शित किया गया है। हालांकि, जब मैं UIImageView को निर्दिष्ट करने से पहले पुनर्प्राप्त यूआईएममेज का पुन: प्रयास करने का प्रयास करता हूं, तो छवि अपेक्षित के रूप में आकार बदल रही है लेकिन उसमें कहीं समस्या है (RESIZING कोड में?) मेरे UIImage को रोका जा रहा है ... नतीजतन, जब मैं आकृति परिवर्तन UIImage एक UIImageView करने के लिए छवि 90 डिग्री घुमाया जाता है और पहलू अनुपात के रूप में (1200 x 1600 पिक्सल) फैला दिखाई देता है अपरिवर्तित था ...कैमरे से खींचे गए UIimages का आकार बदलना UIimage को भी रूट करता है?

मैं इस का उपयोग कर रहा कैमरा से एक UIImage प्राप्त करने के लिए आवंटित:

- (void) imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info 
{ 

     myImg = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
     myResizedImg = [self resizeImage:myImg width:400 height:533]; 
     [myImageView setImage:myResizedImg]; 

} 

मैं इस का उपयोग कर रहा आकार बदलने के लिए:

-(UIImage *)resizeImage:(UIImage *)anImage width:(int)width height:(int)height 
{ 

    CGImageRef imageRef = [anImage CGImage]; 

    CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef); 

    if (alphaInfo == kCGImageAlphaNone) 
    alphaInfo = kCGImageAlphaNoneSkipLast; 


    CGContextRef bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), 4 * width, CGImageGetColorSpace(imageRef), alphaInfo); 

    CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef); 

    CGImageRef ref = CGBitmapContextCreateImage(bitmap); 
    UIImage *result = [UIImage imageWithCGImage:ref]; 

    CGContextRelease(bitmap); 
    CGImageRelease(ref); 

    return result; 
} 

प्रश्न: मैं पिक्सल घूर्णन के बिना कैमरे से खींचा गया UIImage कैसे उपयोग करूं?

उत्तर

60

कारण अपने कोड काम नहीं करता ऐसा इसलिए है क्योंकि छवि आपके पास मौजूद कोड पर ध्यान नहीं दिया जा रहा है। विशेष रूप से, यदि छवि ऑरिएन्टेशन सही/बाएं है, तो आपको छवि को घुमाने और चौड़ाई/ऊंचाई स्वैप करने की आवश्यकता है। ऐसा करने के लिए यहां कुछ कोड दिया गया है:

-(UIImage*)imageByScalingToSize:(CGSize)targetSize 
{ 
    UIImage* sourceImage = self; 
    CGFloat targetWidth = targetSize.width; 
    CGFloat targetHeight = targetSize.height; 

    CGImageRef imageRef = [sourceImage CGImage]; 
    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef); 
    CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef); 

    if (bitmapInfo == kCGImageAlphaNone) { 
     bitmapInfo = kCGImageAlphaNoneSkipLast; 
    } 

    CGContextRef bitmap; 

    if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) { 
     bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo); 

    } else { 
     bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo); 

    } 

    if (sourceImage.imageOrientation == UIImageOrientationLeft) { 
     CGContextRotateCTM (bitmap, radians(90)); 
     CGContextTranslateCTM (bitmap, 0, -targetHeight); 

    } else if (sourceImage.imageOrientation == UIImageOrientationRight) { 
     CGContextRotateCTM (bitmap, radians(-90)); 
     CGContextTranslateCTM (bitmap, -targetWidth, 0); 

    } else if (sourceImage.imageOrientation == UIImageOrientationUp) { 
     // NOTHING 
    } else if (sourceImage.imageOrientation == UIImageOrientationDown) { 
     CGContextTranslateCTM (bitmap, targetWidth, targetHeight); 
     CGContextRotateCTM (bitmap, radians(-180.)); 
    } 

    CGContextDrawImage(bitmap, CGRectMake(0, 0, targetWidth, targetHeight), imageRef); 
    CGImageRef ref = CGBitmapContextCreateImage(bitmap); 
    UIImage* newImage = [UIImage imageWithCGImage:ref]; 

    CGContextRelease(bitmap); 
    CGImageRelease(ref); 

    return newImage; 
} 

यह आपकी छवि का आकार बदल देगा और इसे सही अभिविन्यास पर घुमाएगा। आप रेडियंस के लिए परिभाषा की जरूरत है, यह है:

static inline double radians (double degrees) {return degrees * M_PI/180;} 

जवाब डैनियल दिया भी सही है, लेकिन यह, समस्या यह है कि यह थ्रेड-सुरक्षित नहीं है से ग्रस्त है जब से तुम UIGraphicsBeginImageContext() का उपयोग कर रहे हैं। चूंकि उपरोक्त कोड केवल सीजी कार्यों का उपयोग करता है, इसलिए आप सभी सेट हैं। मेरे पास आकार बदलने के लिए एक समान कार्य भी है और छवियों पर उचित पहलू भरें - अगर आप यही चाहते हैं तो मुझे बताएं।

नोट: मुझे मूल कार्य this post से मिला है, और इसे जेपीईजी पर काम करने के लिए कुछ संशोधन किए हैं।

+0

हां। महान जवाब - धन्यवाद। – RexOnRoids

+0

बस मुझे जो चाहिए (एक अलग परियोजना के लिए)। धन्यवाद! –

+0

धन्यवाद, यह शानदार है! –

11

मैं वहां भी कोड के कुछ के साथ इस समस्या थी, मैं इस कोड है कि काम करता है पाया, चेक करें, मुझे पता है तुम क्या ढूँढते हैं

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

    return newImage; 
} 
+0

यह कोड कैमरा अभिविन्यास को ध्यान में रखता नहीं है। यह छवि को निर्दिष्ट आकार में आसानी से स्केल करता है। – npellow

+0

नहीं, लेकिन आप आसानी से अभिविन्यास के लिए पूछ सकते हैं और वहां उचित आकार भेज सकते हैं ... – Daniel

+2

यह कोड मेरे प्रोजेक्ट में कैमरे के अभिविन्यास को ध्यान में रखना प्रतीत होता है .. – geekinit

1

यदि आप दोनों आयतों में चौड़ाई या ऊंचाई से इसे फसल करके आयताकार छवि (कोई क्षैतिज या लंबवत नहीं) से स्क्वायर छवि बनाना चाहते हैं, तो मेरे कोड का उपयोग करें। अंतर यह है कि मैं खिंचाव नहीं करता, लेकिन मैं फसल। मैं संशोधन द्वारा शीर्ष कोड से इसे बनाया:

//Cropping _image to fit it to the square by height or width 

CGFloat a = _image.size.height; 
CGFloat b = _image.size.width; 
CGRect cropRect; 

if (!(a==b)) { 
    if (a<b) { 
     cropRect = CGRectMake((b-a)/2.0, 0, a, a); 
     b = a; 
    } 
    else if (b<a) { 
     cropRect = CGRectMake(0, (a-b)/2.0, b, b); 
     a = b; 
    } 

    CGImageRef imageRef = CGImageCreateWithImageInRect([_image CGImage], cropRect); 

    UIImage* sourceImage = _image; 
    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef); 
    CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef); 
    CGContextRef bitmap; 
    if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) { 
     bitmap = CGBitmapContextCreate(NULL, a, a, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo); 

    } else { 
     bitmap = CGBitmapContextCreate(NULL, a, a, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo); 
    } 

    if (sourceImage.imageOrientation == UIImageOrientationLeft) { 
     CGContextRotateCTM (bitmap, radians(90)); 
     CGContextTranslateCTM (bitmap, 0, -a); 

    } else if (sourceImage.imageOrientation == UIImageOrientationRight) { 
     CGContextRotateCTM (bitmap, radians(-90)); 
     CGContextTranslateCTM (bitmap, -a, 0); 

    } else if (sourceImage.imageOrientation == UIImageOrientationUp) { 
     // NOTHING 
    } else if (sourceImage.imageOrientation == UIImageOrientationDown) { 
     CGContextTranslateCTM (bitmap, a, a); 
     CGContextRotateCTM (bitmap, radians(-180.)); 
    } 

    CGContextDrawImage(bitmap, CGRectMake(0, 0, a, a), imageRef); 
    CGImageRef ref = CGBitmapContextCreateImage(bitmap); 
    _image = [UIImage imageWithCGImage:ref]; 
    CGImageRelease(imageRef); 
} 
2

स्विफ्ट 3

मैं didFinishPickingMediaWithInfo विधि को यह जोड़ने और फिर अपनी रोटेशन के लिए चिंता किए बिना image का उपयोग करें।

var image = info[UIImagePickerControllerOriginalImage] as! UIImage 
if (image.imageOrientation != .up) { 
    UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale) 
    image.draw(in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)) 
    image = UIGraphicsGetImageFromCurrentImageContext()! 
    UIGraphicsEndImageContext() 
} 
संबंधित मुद्दे