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;
Related
I am trying to take a screen shot of view inside a cell of UITableView but with attached code I can able only to take a screenshot of cell bounds. The problem is that by UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f). I can only make screenshot of rect size and cannot control origin of rect and
rect = [cell bounds]. so please suggest me some idea.
{
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:path];
__block CGRect rect = [cell bounds];
UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[cell.layer renderInContext:context];
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
Take the screenShot as a normal screenShot and then crop it
-(UIImage *)cropImage:(UIImage *)image rect:(CGRect)cropRect
{
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return img;
}
Use like this:
UIImage *img = [self cropImage:viewImage rect:CGRectMake(150,150,100,100)];
To get the frame of a particular tableView Cell. Use this:
CGRect myRect = [tableView rectForRowAtIndexPath:0];//example 0,1,indexPath
Hope this helps. Refer to this link link2
- (UIImage *) screenshot {
CGSize size = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
CGRect rec = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); //set the frame
[self.view drawViewHierarchyInRect:rec afterScreenUpdates:YES];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
I'm trying to download some image files from a remote server through the AFNetworking (UIImageView+AFNetworking.h) & retrieve the UIImage from it, edit that image. Editing means add that image(downloaded image) on top of another png file.( background image - UIImage )
I have tried several code blocks finally I'm stuck in here . I'm getting a black box only. Can't see the actual server image .
-(UIImage *)downloadImages:(NSString *)url{
UIImageView *downloadedImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,40,40)];
[downloadedImageView setImageWithURL:[NSURL URLWithString:url]
placeholderImage:[UIImage imageNamed:#"Loading_image"]];
UIGraphicsBeginImageContextWithOptions(downloadedImageView.bounds.size, downloadedImageView.opaque, 0.0);
[downloadedImageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
Then I'm calling through that function in a for loop.
for( int i=0;i<[Images count];i++){
NSString *image_Url = [NSString stringWithFormat:#"%#%#",imageURL, imagename];
UIImage *downloadimage =[[UIImage alloc]init];
downloadimage = [self downloadImages:image_Url];
UIImage *bottomImage = [UIImage imageNamed:#"map_marker_black"];
CGSize newSize = CGSizeMake(60, 60);
UIGraphicsBeginImageContext( newSize );
[backgroundImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
[downloadimage drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.7];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
Where am I doing wrong ? Your help is highly appreciated. Thanks a lot
A few issues. Lets start with the download code. There is no reason to deal with an image view and drawing it to create an image. Just use the actual image:
- (UIImage *)downloadImage:(NSString *)url{
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
UIImage *image = [UIImage imageWithData:imageData];
return image;
}
The big issue with your original code is that the AFNetworking method you used runs in the background. So you were always trying to draw the "loading" image.
Now the drawing code:
// Only need to load this once
UIImage *backgroundImage = [UIImage imageNamed:#"map_marker_black"];
// What is the loop used for?
for (NSUInteger i = 0; i < [Images count]; i++){
// Where does imageURL and imagename come from?
NSString *imageURL = [NSString stringWithFormat:#"%#%#",imageURL, imagename];
UIImage *downloadImage = [self downloadImage:imageURL];
CGRect newRect = CGRectMake(0, 0, 60, 60);
UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0);
[backgroundImage drawInRect:newRect];
[downloadimage drawInRect:newRect blendMode:kCGBlendModeNormal alpha:0.7];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// Do something here with newImage
UIGraphicsEndImageContext();
}
Note the comments in the code.
I am trying to make an Xcode app that has a list that loads data from a JSON file. It basically has a title, subtitle, and a thumbnail image. When the app loads up, the images are exactly as big as the cells. I would either like to resize the images to a set size, or make some extra room in-between the cells. Here is my current code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *tempDictionary= [self.googlePlacesArrayFromAFNetworking objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:[tempDictionary objectForKey:#"icon"]];
NSData *data = [NSData dataWithContentsOfURL:url];
cell.imageView.image = [UIImage imageWithData:data];
CALayer * l = [cell.imageView layer];
[l setCornerRadius:13];
[l setBorderWidth:0.5];
[l setBorderColor:[[UIColor lightGrayColor] CGColor]];
cell.imageView.layer.masksToBounds = YES;
...}
Thanks for your help.
Update: I have discovered this:
UIImage *thumbnail = [UIImage imageWithData: [NSData dataWithContentsOfURL :url]];
CGSize itemSize = CGSizeMake(30, 30);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(30.0, 30.0, itemSize.width, itemSize.height);
[thumbnail drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
The image resizes, but it is just blank.
I was facing same problem with image re-sizing. After searching on internet I found a useful 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;
}
This will return the image size.
In my case I made a Helper class and in Helper.h I declared this method as a class method so that I can call it in my UITableView. An than in Helper.m I implemented this method.
In cellForRowAtIndexPath method:
cell.imageView.image = [Helper imageWithImage:***Your Image here*** scaledToSize:CGSizeMake(ImageWidth,ImageHeight)];
Don't forget to import Helper.h in your TableView class.
You can also use this class in your way.
Hope it works.
Try this:
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
Or:
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
Or, you could scale the images that you get from the url.
CGFloat scale = 10.0f; // adjust this number
UIImage *image = [UIImage imageWithData:
[NSData dataWithContentsOfURL:url]
scale:scale];
cell.imageView.image = image;
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.
rotate image merge but not working in this code
i post my code
please give me solution
- (void)mergeImage :(UIImage *)imageA withImage:(UIImage *)imageB ###
{
UIImage *image0 = imageA;
UIImage *image1=overlayimg.image;
CGSize newImageSize = CGSizeMake(overlayimg.frame.size.width, overlayimg.frame.size.height);
UIGraphicsBeginImageContext(newImageSize);
[image0 drawInRect:CGRectMake(overlayimg.frame.origin.x,overlayimg.frame.origin.y,newImageSize.width,newImageSize.height)];
NSLog(#"Last Rotation:%f",templastrotate);
overlayimg.transform = CGAffineTransformMakeRotation(templastrotate);
CGContextConcatCTM(UIGraphicsGetCurrentContext(), overlayimg.transform);
[image1 drawInRect:CGRectMake(overlayView.frame.origin.x, overlayView.frame.origin.y,overlayView.frame.size.width,overlayView.frame.size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
imageView.contentMode = UIViewContentModeScaleAspectFill & UIViewContentModeScaleAspectFit;
imageView.image=newImage;
UIGraphicsEndImageContext();
}