2011-06-17 13 views
7

में UIImage का आकार बदलना मैं इसमें कुछ छवियों के साथ एक यूआईपीकर व्यू बनाने की कोशिश कर रहा हूं, लेकिन मुझे लगता है कि छवियों को दृश्य में फिट करने के तरीके को कैसे प्राप्त किया जा सकता है (अभी वे बहुत बड़े हैं और हैं एक दूसरे को ओवरलैप करना)।UIImageView

मैं प्रत्येक छवि को आकार देने के लिए एक फ़ंक्शन का उपयोग करने का प्रयास कर रहा हूं, लेकिन जब फ़ंक्शन कहा जाता है तो मुझे त्रुटियां मिल रही हैं, हालांकि प्रोग्राम संकलित करता है और ठीक चलाता है (छवि का आकार बदलने का अपवाद नहीं है)। आकार बदलने समारोह और आरंभीकरण कार्य हैं:

-(UIImage *)resizeImage:(UIImage *)image width:(int)width height:(int)height { 
    NSLog(@"resizing"); 
    CGImageRef imageRef = [image 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; 
} 

- (void)viewDidLoad { 
    UIImage *h1 = [UIImage imageNamed:@"h1.png"]; 

    h1 = [self resizeImage:h1 width:50 height: 50]; 

    UIImageView *h1View = [[UIImageView alloc] initWithImage:h1]; 

    NSArray *imageViewArray = [[NSArray alloc] initWithObjects: 
           h1View, nil]; 

    NSString *fieldName = [[NSString alloc] initWithFormat:@"column1"]; 
    [self setValue:imageViewArray forKey:fieldName]; 
    [fieldName release]; 
    [imageViewArray release]; 

    [h1View release]; 
} 

कंसोल आउटपुट:

TabTemplate [29322: 207] का आकार बदलने

TabTemplate [29322]: CGBitmapContextCreate: असमर्थित colorspace

TabTemplate [ 2 9 322]: CGContextDrawImage: अमान्य संदर्भ

टैबटेम्प्ला ते [2 9 322]: CGBitmapContextCreateImage: अमान्य संदर्भ

मैं यह नहीं समझ सकता कि क्या गलत हो रहा है। कोई भी मदद बहुत ही सराहनीय होगी।

+0

के मामले में आप की जाँच किया था अगर CGImageGetColorSpace (imageRef) द्वारा दिए गए मान मान CGBitmapContextCreate द्वारा स्वीकार में से एक है? – Bemmu

उत्तर

16

यदि आप की contentMode संपत्ति का उपयोग करते हैं तो आपको अपने UIImage का आकार बदलने की आवश्यकता नहीं है।

myImageView.contentMode = UIViewContentModeScaleAspectFit; 

या यदि आप अभी भी अपने यूआईएममेज का आकार बदलना चाहते हैं, तो नीचे पोस्ट देखें।

resizing a UIImage without loading it entirely into memory?

UIImage: Resize, then Crop

15

पहलू अनुपात का उपयोग कर छवि पैमाने पर करने के लिए नीचे दिए गए का उपयोग करें, तो imageView के परिबंधों पर छवि क्लिप।

imageView.contentMode = UIViewContentModeScaleAspectFill; 
imageView.clipsToBounds = YES; 
+1

इस उत्तर के लिए धन्यवाद। 'क्लिप्सबाउंड्स' मैं जो खो रहा था - बड़ी मदद! – Sam

+0

@ सैम - आपका स्वागत है :) – trillions

+0

यह पहलू अनुपात को ध्यान में रखते हुए छवि के साथ दृश्य को सही ढंग से भर देगा लेकिन क्षेत्र में फिट नहीं होने पर छुपाता है। –

0

तेज

imageView.contentMode = .ScaleAspectFill 
imageView.clipsToBounds = true 
-1
UIImage *image = [UIImage imageNamed:@"myImage"]; 
    [image drawInRect: destinationRect]; 
    UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext(); 
UIImageWriteToSavedPhotosAlbum(image,nil,nil,nil);