Make UITableView imageView square? - objective-c

I have a app that will populate a UITableView after the user takes a picture.
The UITableViewCell's image is that picture that the user takes. I would prefer these images to be square.
I have tried just about everything, here's some code:
UIImage *theImage = [UIImage imageNamed:theImagePath];
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.image = theImage;
Thanks!
Coulton

one option would be to pass in a square image:
UIGraphicsBeginImageContext(CGSizeMake(60.0f, 60.0f));
[fullSizeImage drawInRect:CGRectMake(0,0,60.0f, 60.0f)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
you would probably want to only do this once for each image, and then cache the result.

Related

UIImageView is resized as per UIImage - Objective C

I am making UIImage from CGIImageRef.
CGImageRef drawImage = CGImageCreateWithImageInRect(croppedImage.CGImage, frame);
UIImage *newImage = [UIImage imageWithCGImage:drawImage];
CGImageRelease(drawImage);
And applying it to Image View:
self.imageview.image = newImage;
After this line self.imageview.image = imgV.image; my imageView is resized to size of UIImage(newImage).
When I log my self.imageview frame then I am getting correct value. I am not able to get exact reason behind this.
UIImageView will be autoResize to fit its image size if u didnt set the frame of imageView

Crop UIImageView's image in scale to fill mode

I have one UIImageView which is in scale to fill mode. Now i want to crop the image in imageview. I have one rectangle on this imageview which user can move and change the size. After particular size is selected i want to crop the image using this frame. But since my mode of uiimageview is scale to fill this messes the cropping rectangle and different part is cropped. Has any one faced this problem. How to crop properly ?
Your options are to calculate, or use this walkaround:
-(UIImage *)imageFromImageView:(UIImageView *)view withCropRect:(CGRect)cropRect
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * largeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect);
UIImage* img = UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return img;
}

Save uiview as specified size

I have an uiimageview that is 600x600 and a uitexview loaded in a 200x200 uiview.
I would like to export this uiview to 600x600, same as the uiimageview size.
However If I use the code below I can only generate a size of 400x400, that is the retina size of the uiview.
Is there any code I can achieve this?
UIGraphicsBeginImageContextWithOptions(outputView.bounds.size, outputView.opaque, 2);
[outputView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Have you tried changing the bounds of the view to what you need?
outputView.bounds = CGRectMake(0,0,600,600);
UIGraphicsBeginImageContextWithOptions(outputView.bounds.size, outputView.opaque, 2); [outputView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
outputView.bound = ... original bounds here ...
If the internal view is set to be resized automatically, this should work seamlessly. Otherwise, you could change the frame of the internal view as well.
You could use the UIImage category of AliSoftware to resize it

Scale UIImageView relatively

I've got two UIImageViews, I've managed to merge and save them. But I want the overlaying image to scale and position relatively with the scale of the chosen image. So that the UIImageView that you see on the screen looks the same as the UIImageView which it saves.
- (UIImage *)combineImages{
UIGraphicsBeginImageContext(CGSizeMake(theimageView.image.size.width,theimageView.image.size.height));
// Draw image1
[theimageView.image drawInRect:CGRectMake(0,0, theimageView.image.size.width,theimageView.image.size.height)];
// Draw image2
[Birdie.image drawInRect:CGRectMake(Birdie.center.x, Birdie.center.y, (theimageView.image.size.width / 2), (theimageView.image.size.height / 2))];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
}
theImageView and Birdie are the two UIImageViews.
Thanks in advanced!
Please Try this,
- (UIImage *)combineImages{
// Draw image1
[theimageView.image drawInRect:CGRectMake(0,0, theimageView.image.size.width,theimageView.image.size.height)];
// Draw image2
[Birdie.image drawInRect:CGRectMake(Birdie.frame.origin.x, Birdie.frame.origin.y, Birdie.frame.size.width, Birdie.frame.size.height)];
//have you added Birdie over theimageView if not than do this. If your size is already set as you want then there is no need for above two lines of drawInRect.
UIGraphicsBeginImageContext(theimageView.bounds.size);
[theimageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
}
Thanks,

UITableViewCell's imageView fit to 40x40

I use the same big images in a tableView and detailView.
Need to make imageView filled in 40x40 when an imags is showed in tableView, but stretched on a half of a screen. I played with several properties but have no positive result:
[cell.imageView setBounds:CGRectMake(0, 0, 50, 50)];
[cell.imageView setClipsToBounds:NO];
[cell.imageView setFrame:CGRectMake(0, 0, 50, 50)];
[cell.imageView setContentMode:UIViewContentModeScaleAspectFill];
I am using SDK 3.0 with build in "Cell Objects in Predefined Styles".
I put Ben's code as an extension in my NS-Extensions file so that I can tell any image to make a thumbnail of itself, as in:
UIImage *bigImage = [UIImage imageNamed:#"yourImage.png"];
UIImage *thumb = [bigImage makeThumbnailOfSize:CGSizeMake(50,50)];
Here is .h file:
#interface UIImage (PhoenixMaster)
- (UIImage *) makeThumbnailOfSize:(CGSize)size;
#end
and then in the NS-Extensions.m file:
#implementation UIImage (PhoenixMaster)
- (UIImage *) makeThumbnailOfSize:(CGSize)size
{
UIGraphicsBeginImageContextWithOptions(size, NO, UIScreen.mainScreen.scale);
// draw scaled image into thumbnail context
[self drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *newThumbnail = UIGraphicsGetImageFromCurrentImageContext();
// pop the context
UIGraphicsEndImageContext();
if(newThumbnail == nil)
NSLog(#"could not scale image");
return newThumbnail;
}
#end
I cache a thumbnail version since using large images scaled down on the fly uses too much memory.
Here's my thumbnail code:
- (UIImage *)thumbnailOfSize:(CGSize)size {
if( self.previewThumbnail )
return self.previewThumbnail; // returned cached thumbnail
UIGraphicsBeginImageContext(size);
// draw scaled image into thumbnail context
[self.preview drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *newThumbnail = UIGraphicsGetImageFromCurrentImageContext();
// pop the context
UIGraphicsEndImageContext();
if(newThumbnail == nil)
NSLog(#"could not scale image");
self.previewThumbnail = newThumbnail;
return self.previewThumbnail;
}
Just make sure you properly clear the cached thumbnail if you change your original image (self.preview in my case).
I have mine wrapped in a UIView and use this code:
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;
[self addSubview:imageView];
imageView.frame = self.bounds;
(self is the wrapper UIView, with the dimensions I want - I use AsyncImageView).
I thought Ben Lachman's suggestion of generating thumbnails in advance rather than on the fly was smart, so I adapted his code so it could handle a whole array and to make it more portable (no hard-coded property names).
- (NSArray *)arrayOfThumbnailsOfSize:(CGSize)size fromArray:(NSArray*)original {
NSMutableArray *temp = [NSMutableArray arrayWithCapacity:[original count]];
for(UIImage *image in original){
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0,0,size.width,size.height)];
UIImage *thumb = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[temp addObject:thumb];
}
return [NSArray arrayWithArray:temp];
}
you might be able to use this?
yourTableViewController.rowImage = [UIImage imageNamed:#"yourImage.png"];
and/or
cell.image = yourTableViewController.rowImage;
and if your images are already 40x40 then you shouldn't have to worry about setting bounds and stuff... but, i'm also new to this, so, i wouldn't know, haven't played around with Table View row/cell images much
hope this helps.
I was able to make this work using interface builder and a tableviewcell. You can set the "Mode" properties for an image view to "Aspect Fit". I'm not sure how to do this programatically.
Try setting UIImageView.autoresizesSubviews and/or UIImageView.contentStretch.