8

I'm trying to create a UIPickerView with some images in it, but I can't seem to figure out how to get the images to fit in the view (right now they're too large and are overlapping each other).

I'm trying to use a function to resize each image when it's drawn, but I'm getting errors when the function is called, although the program compiles and runs fine (with the exception of the image not resizing). The resizing function and initialization functions are:

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

Console Output:

TabTemplate[29322:207] resizing

TabTemplate[29322] : CGBitmapContextCreate: unsupported colorspace

TabTemplate[29322] : CGContextDrawImage: invalid context

TabTemplate[29322] : CGBitmapContextCreateImage: invalid context

I can't figure out what's going wrong. Any help is greatly appreciated.

Community
  • 1
  • 1
Paul Woidke
  • 928
  • 4
  • 18
  • 40
  • Did you check if the value returned by CGImageGetColorSpace(imageRef) is one of the values accepted by CGBitmapContextCreate? – Bemmu Jun 17 '11 at 05:20

4 Answers4

17

You don't require to resize your UIImage if you use the contentMode property of UIImageView.

    myImageView.contentMode  = UIViewContentModeScaleAspectFit;

Or if you still want to resize your UIImage, Have look at below SO post.

resizing a UIImage without loading it entirely into memory?

UIImage: Resize, then Crop

Community
  • 1
  • 1
Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
14

Use below to scale the image using aspect ratio, then clip the image to imageview's bounds.

imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES; 
max_
  • 24,076
  • 39
  • 122
  • 211
trillions
  • 3,669
  • 10
  • 40
  • 59
0

In case of swift

imageView.contentMode = .ScaleAspectFill
imageView.clipsToBounds = true
Ghulam Rasool
  • 3,996
  • 2
  • 27
  • 40
-1
UIImage *image = [UIImage imageNamed:@"myImage"];
    [image drawInRect: destinationRect];
    UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(image,nil,nil,nil);
James Bush
  • 1,485
  • 14
  • 19