Resize UIImage in UIImageView

I am trying to create a UIPickerView with some images in it, but I cannot figure out how to get the images according to the view (now they are too big and overlap each other).

I try to use a function to resize each image when drawing, but I get errors when calling the function, although the program compiles and works fine (except for the image without resizing). Resize function and initialization function:

-(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] resize

TabTemplate [29322]: CGBitmapContextCreate: unsupported color space

TabTemplate [29322]: CGContextDrawImage: invalid context

TabTemplate [29322]: CGBitmapContextCreateImage: invalid context

I can not understand what is happening. Any help is appreciated.

+7
source share
4 answers

You do not need to resize the UIImage if you use the contentMode property of the UIImageView .

  myImageView.contentMode = UIViewContentModeScaleAspectFit; 

Or, if you still want to resize your UIImage, see the SO post below.

resizing UIImage without fully loading it into memory?

UIImage: Resize, then Crop

+16
source

Use below to scale an image using aspect ratio, then copy the image to the image frames.

 imageView.contentMode = UIViewContentModeScaleAspectFill; imageView.clipsToBounds = YES; 
+15
source

In case of fast

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

All Articles