Image not coming in UIImageView - ios

I added a UIImageView to my application using an IBOutlet, then assigned an image programmatically, but the image is not displaying. My code is shown below:
NSString *string = #"Some text";
UIGraphicsBeginImageContext(CGSizeMake(180, 50));
[string drawAtPoint:CGPointMake(10, 20)
withFont:[UIFont systemFontOfSize:12]];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//[imageview setImage:result];
//[imageview
sample.image =result;

Related

Sub Views are not visible after change the UILabel Background colour

I have changed my UILabel background colour like this. Actually I have added an image to it. I did it like this.
self.lblMsg=[[UILabel alloc] initWithFrame:CGRectMake(8.0, self.lblDateTime.frame.origin.y+self.lblDateTime.frame.size.height, size.width-5.0, 100.0)];
UIImage *img = [UIImage imageNamed:#"msgBackground"];
CGSize imgSize = self.lblMsg.frame.size;
UIGraphicsBeginImageContext( imgSize );
[img drawInRect:CGRectMake(0,0,imgSize.width,imgSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.lblMsg.backgroundColor = [UIColor colorWithPatternImage:newImage];
[self.contentView addSubview:self.lblMsg];
Now I want to add another UILabelon to this and that label should include an image as an attachment. So I did it like this.
self.lblWeatherTitle=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, self.lblMsg.frame.size.width, 50.0)];
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:#"splashLogo"];
NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
NSMutableAttributedString *myString= [[NSMutableAttributedString alloc] initWithString:#"My label text"];
[self.lblWeatherTitle setTextColor:[UIColor redColor]];
[myString appendAttributedString:attachmentString];
self.lblWeatherTitle.attributedText = myString;
[self.lblMsg addSubview:self.lblWeatherTitle];
But My 2nd UILabel is not visible.
This is what I need to do
This black text is a long description. As the title of it it should show those red label and the yellow icon. so I added that top label as a subview to this white label. This whole thing is inside a custom UITableViewCell
Please help me. How can I do this in correct way?
Thanks

Clickable events in NSAttributedString in ios

i had inserted UIimage inside uitextview as NSTextAttachment using nsattributedstring.
Now because of some default property for attrbutestring i am getting long press event on my image.
i.e When i do long press in UIImage it got UIActionsheet giving me option to "SAVE IMAGE" or "COPY".
I had not written any code for this..
What i actully want on the long press of UIIMAGE is i want to open that small UIIMAGE into my full screen like we have in whatapp like chatting.
When user send some image and user click on that image it get displayed in full screen.
Any one please help me to customise this click event of nstextattachment.
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:#"Somestring"];
CGRect rect = CGRectMake(0,0,100,100);
UIGraphicsBeginImageContext( rect.size );
[image drawInRect:rect];
UIImage *picture1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(picture1);
UIImage *img=[UIImage imageWithData:imageData];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image =img;
NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
[attributedString replaceCharactersInRange:NSMakeRange(0,0) withAttributedString:attrStringWithImage];
UITextView *messageContentView=[[UITextView alloc] init];
messageContentView.attributedText=attributedString;

I want to know which image is pressed in UIScrollView in objective c

I have UIScrollView that consists of UIImageViews. When I touch any imageView, I want to display this UIImageView's image in other window. how can I do that? any ideas .
I use UITapGestureRecognizer but I cant find which imageView is pressed.
this is my code:
for (NSArray *photo in self.photos) {
imageName = [photo valueForKey:#"filename"];
imageName = [imageName stringByAppendingString:#".jpg"];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView;
imageView = [[UIImageView alloc] initWithImage:image];
[imageView setFrame:CGRectMake(imageX, imageY, imageSize, imageSize)];
imageX += imageSize + 10;
if (imageX + imageSize >= self.view.bounds.size.width){
imageX = 15;
imageY += imageSize + 10;
}
imageView.userInteractionEnabled = YES;
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(goFullPhotoGallery:)];
[imageView addGestureRecognizer:tap];
[scrollView addSubview:imageView];
}
for this type of task i generally use a UIButton over image view and addTarget to them and get image with button tag.
A better solution would be to use a UITableView or a UICollectionView, and listen to cell selection events...
Try this -
for (NSArray *photo in self.photos) {
imageName = [photo valueForKey:#"filename"];
imageName = [imageName stringByAppendingString:#".jpg"];
UIImage *image = [UIImage imageNamed:imageName];
UIButton *starBtn = [[UIButton alloc]initWithFrame:CGRectMake(imageX, imageY, imageSize, imageSize)];
[starBtn setBackgroundColor:[UIColor clearColor]];
[starBtn setImage:image forState:UIControlStateNormal];
[starBtn setTag:i+1];
[starBtn setTitle:imageName forState:UIControlStateNormal];
[starBtn setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[starBtn addTarget:self action:#selector(goFullPhotoGallery:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:starBtn];
}
and goFullPhotoGallery function like this
-(void)goFullPhotoGallery:(UIButton *)imgView{
NSString *imgName = imgView.titleLabel.text;
}

Scrollview not shwoing images

i have retrived images from document directory and want to display that images in scrollview. the images is coming from the document directory but not displaying in scrollview. i have the below code. I have put scrollview graipically on xib and also creat outlet..
self.scrollView.contentSize = CGSizeMake(320, 136);
self.scrollView.delegate = self;
int X=0;
for (int i = 0; i < [imageFileNames count]; i++)
{
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%d/%d.png",b, i]];
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
[images addObject:img];
NSLog(#"%#",getImagePath);
//imge.image=img;
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(X, 0, 100, 100)] ;
[imageView setImage: [UIImage imageNamed:[NSString stringWithFormat:#"%d.png", i]]];
[self.scrollView addSubview: imageView];
X = X + imageView.frame.size.height+5;
if(X > 320)
self.scrollView.contentSize = CGSizeMake(X, 140);
}
Seems you are trying to create the UIImageView from images that are located in your resources (but they are not there!).
[imageView setImage: [UIImage imageNamed:[NSString stringWithFormat:#"%d.png", i]]];
whats the point of placing the images in a array, if you dont use it?
try this instead:
[imageView setImage: [images objectAtIndex:i]];

merging uilabel and uiimageview

I picked an image from the photo album and put it in an UIImageView called imageView
I then added some text over the image using UITextView as source of input and a UILabel on top of the uiimageview to display the text.
So far so good.
Now I'm trying to merge the label and the imageview in order to do other stuff with that but I 'm not getting it to work. I have tried some solutions given to similar questions but they didn't work for me.
I tried this
UIGraphicsBeginImageContextWithOptions(_imageView.bounds.size, NO, 0.0); //retina res
[self.imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
[_displaylabel.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
it tells me that Received type "CAlayer for instance message is a forward declaration".
any other suggestions?
Try this:
UIGraphicsBeginImageContextWithOptions(_imageView.bounds.size, NO, 0.0); //retina res
[self.imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Make sure that the UILabel is a subview of your UIImageView, so that rendering the layer will include all sublayers (the UILabel included). It seems like you forgot to include UIGraphicsEndImageContext(); too, which may have been the issue
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]];
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, imgView.frame.size.width, imgView.frame.size.height)];
[tempView addSubview:imgView];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(tempView.frame.size.width/2, 0, 200, tempView.frame.size.height/2)];
[label setText:#"Hello... You there"];
[label setTextColor:[UIColor blackColor]];
[label setTextAlignment:NSTextAlignmentLeft];
[label setBackgroundColor:[UIColor clearColor]];
[tempView addSubview:label];
UIGraphicsBeginImageContext(tempView.bounds.size);
[tempView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Resources