I have a cell with an image. I am trying to change the size of the image in the cell programmatically like this:
cell.myImage.image = [UIImage imageNamed:#"my_icon"];
CGSize size = CGSizeMake(10, 10);
[self imageWithImage:cell.goalTypeImage.image scaledToSize:size];
Image with size looks like this:
- (UIImage*)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
I also tried this which does not work:
cell.myImage.frame = CGRectMake(
cell.myImage.frame.origin.x,
cell.myImage.frame.origin.y, 50, 50);
cell.myImage.image = [UIImage imageNamed:#"icon"];
Does anyone know why the image is NOT changing size?
You're not doing anything with the returned image. This should work:
CGSize size = CGSizeMake(10, 10);
cell.myImage.image = [self imageWithImage:cell.goalTypeImage.image scaledToSize:size];
I think that you should use the native image of UITableViewCell like this :
cell.imageView.image = myImage;
And then you can resize it :
cell.imageView.transform = CGAffineTransformMakeScale(widthScale, heightScale);
You have some interesting UITableViewCell's properties like textLabel, detailTextLabel, and imageView.
Related
This is my code:
UIImage *image1 = [UIImage imageNamed:#"AllMyFiles-1 (dragged).tiff"];
cell.imageView.image = image1;
cell.textLabel.text = self.greekLetters[indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
And this is is what it looks like:
tableview with image
And I want the UIImage to be a bit smaller much like this:
tableview with smaller image
There are some options to solve that...
1) You can put transparency in your image,
getting something like that: image centered with transparency
2) You can draw your custom cells prototype on your StoryBoard or Xib file
3) You can resize your image programmatically:
Add this method on your class or a extension of UIImage:
- (UIImage *) reziseImage:(UIImage*) image withSize:(CGSize) newSize {
UIGraphicsBeginImageContext(newSize);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
And call it on your method:
UIImage *image1 = [UIImage imageNamed:#"AllMyFiles-1 (dragged).tiff"];
cell.imageView.image = image1;
CGSize newSize = CGSizeMake(25, 25);
image1 = [self reziseImage:image1 withSize:newSize];
cell.imageView.image = image1;
cell.textLabel.text = self.greekLetters[indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
I have a cropped image & i display it on ui.When i add frame to it then image size is changed. for eg.if image size is 200*50 & when i add another image it on as a frame then the new image becomes equal height & width of the frame image. How can I get the solution for this?
Here is my code:
-(void)addFrames:(int)count
{
float scale ;
UIImage *borderImage;
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)] == YES && [[UIScreen mainScreen] scale] == 2.00)
{
// RETINA DISPLAY
scale = 2.0;
}
else
{
// non - retina display
scale = 1.0;
}
if(count==1)
{
borderImage = [UIImage imageNamed:#""];
}
else if(count==2)
{
borderImage = [UIImage imageNamed:#"borderImage1.png"];
}
else if(count==3)
{
borderImage = [UIImage imageNamed:#"borderImage2.png"];
}
else if(count==4)
{
borderImage =[UIImage imageNamed:#"borderImage3.png"];
}
else if(count==5)
{
borderImage = [UIImage imageNamed:#"borderImage4.png"];
}
UIImage *bottomImage = self.mainImage; //background image
UIImage *image = borderImage; //foreground image
CGSize newSize = CGSizeMake(self.img_background.frame.size.width, self.img_background.frame.size.height);
UIGraphicsBeginImageContext( newSize );
// Use existing opacity as is
[bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Apply supplied opacity if applicable
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.8];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();//
self.img_background.image=newImage;
framedImage=newImage;
}
I want the new framed image to be of equal size as it was before adding the frame?
You are the one who is saying:
[bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
That stretches the image to fill this rect, so it is newSize.width by newSize.height. If that isn't what you want, then don't do that. It draws where you tell it to draw at the size you tell to it to draw.
I have a NSMutableArray that contain several UIImage objects, these objects contain different dimensions and they all need to have the width of 140, but that will make the images really awful in my UICollectionView.
Is there a method where i put in the UIImage and then set the width 140 and then get the new height, which i need for the cell height?
Working code:
//create image array
UIImage *image = [UIImage imageNamed:#"yourimagename.png"];
NSArray *imageArray = [NSArray arrayWithObjects:image, nil];
//get image original height, original width...
CGFloat originalHeight = image.size.height;
CGFloat originalWidth = image.size.width;
//set wanted width and calculate wanted height...
CGFloat wantedWidth = 140;
CGFloat wantedHeight = (originalHeight*wantedWidth)/originalWidth;
//call resize image method... setting wanted height and width....
UIImage *avatarImage = [self imageWithImage:[imageArray objectAtIndex:0] scaledToSize:CGSizeMake(wantedWidth, wantedHeight)];
//create imageView.. ... .
UIImageView *avatarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, wantedWidth, wantedHeight)];
[avatarImageView setImage:avatarImage];
[self.view addSubview:avatarImageView];
Resize image method.
- (UIImage *)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
UITableView has UIImageView, all images has the same size. But when I starting scroll or selecting row, some images can change size. I cannot understand why. Or how to make an imageview of a fixed size in a table row?
Use UIGraphicsImageContext with the table view cell image view like :
UIImage *thumbNail = [UIImage imageNamed:[_imageArray objectAtIndex:indexPath.row]];
// Setting thumbnail image
CGSize itemSize = CGSizeMake(60, 40);// Sample image size
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[thumbNail drawInRect:imageRect];
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
You can do one thing.. Use custom cell and set imageView's frame there.
I am trying to enter MPMediaItemArtwork Image into UITableView's cell's ImageView with following code.
MPMediaItemArtwork *artwork = [[[self.arrayOfAlbums objectAtIndex:indexPath.row] representativeItem]valueForProperty:MPMediaItemPropertyArtwork];
UIImage *artworkImage = [artwork imageWithSize: cell.imageView.bounds.size];
if (artworkImage)
{
cell.imageView.image = artworkImage;
}
else
{
cell.imageView.image = [UIImage imageNamed: #"noArtwork.png"];
}
It's okay when i insert Artwork image in UITableView's cell ImageView.
But when my artwork image is too small or big , it's happened like following pic.
Not in completely fill in cell's ImageView.
You see that? I want to set Fill with Stretch like iOS Music App
Here is Build-in App Artwork Image that set Completely fill with Stretch
I want to do like that.
So i used cell.imageView.contentMode = UIViewContentModeScaleAspectFill; however it's not effect.
So how can i do that?
Thanks you for your work.
Try this for calculating a new and fitting image;
Adjust newRect to the rect of your cell.
// scale and center the image
CGSize sourceImageSize = [artworkImage size];
// The rectangle of the new image
CGRect newRect;
newRect = CGRectMake(0, 0, 40, 33);
// Figure out a scaling ratio to make sure we maintain the same aspect ratio
float ratio = MAX(newRect.size.width / sourceImageSize.width, newRect.size.height / sourceImageSize.height);
UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 1.0);
// Center the image in the thumbnail rectangle
CGRect projectRect;
projectRect.size.width = ratio * sourceImageSize.width;
projectRect.size.height = ratio * sourceImageSize.height;
projectRect.origin.x = (newRect.size.width - projectRect.size.width) / 2.0;
projectRect.origin.y = (newRect.size.height - projectRect.size.height) / 2.0;
[sourceImage drawInRect:projectRect];
UIImage *sizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.image = sizedImage;
The line
UIImage *artworkImage = [artwork imageWithSize: cell.imageView.bounds.size];
creates an image with the size of the cell. So, it's scaled in this line and the image won't be bigger than the cell and therefore it will not scale afterwards.
I would leave the image at it's original size or at a size 2x the cell size and leave the scaling up to contentMode.
CGSize newSize = CGSizeMake(artwork.bounds.size.width, artwork.bounds.size.height)
UIImage *artworkImage = [artwork imageWithSize: newSize];