I have following code that takes an image form photo gallery using UIImagePickerController
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *pickerImg = info[UIImagePickerControllerEditedImage];
if (pickerImg == nil) {
pickerImg = info[UIImagePickerControllerOriginalImage];
}
NSData *imageData = UIImagePNGRepresentation(pickerImg);
UIImage *img = [UIImage imageWithData:imageData];
UIImage *fixedOrientationImage = [UIImage imageWithCGImage:img.CGImage scale:pickerImg.scale orientation:pickerImg.imageOrientation];
pickerImg = fixedOrientationImage;
cropView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
cropView.backgroundColor = [UIColor darkTextColor];
[self.navigationController setNavigationBarHidden:YES animated:YES];
UIImageView * cropImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 60)];
cropImageView.image = pickerImg;
cropImageView.contentMode = UIViewContentModeScaleAspectFit;
cropImageView.clipsToBounds = YES;
[cropView addSubview:cropImageView];
[self.view addSubview:cropView];
}
I have selected the following image
that is shown over above cropImageView like this
Try setting either the image or the frame after setting the content mode
UIImageView * cropImageView = [[UIImageView alloc] init];
cropImageView.contentMode = UIViewContentModeScaleAspectFit;
cropImageView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 60);
cropImageView.image = pickerImg;
cropImageView.clipsToBounds = YES;
Related
I can add image asynchronously seperately by using UIImageView+AFNetworking
[imageView setImageWithURL:[NSURL URLWithString:[self.data valueForKey:#"image_link"]]];
I can also add image to UITextView by
UITextView *text = [[UITextView alloc] init];
UIImageView *imageView = [UIImageView new];
[imageView setImage:[UIImage imageNamed:#"image.png"]];
CGRect aRect = CGRectMake(0, 0, 125, 120);
[imageView setFrame:aRect];
UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:aRect];
text.textContainer.exclusionPaths = #[exclusionPath];
[text addSubview:imageView];
But I cannot add image asynchronously in UITextView like
UITextView *text = [[UITextView alloc] init];
UIImageView *imageView = [UIImageView new];
[imageView setImageWithURL:[NSURL URLWithString:[self.data valueForKey:#"image_link"]]];
CGRect aRect = CGRectMake(0, 0, 125, 120);
[imageView setFrame:aRect];
UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:aRect];
text.textContainer.exclusionPaths = #[exclusionPath];
[text addSubview:imageView];
So how can I add image asynchronously in UITextView?
Use a dispatch :
dispatch_async(dispatch_get_main_queue(), ^{
// async UI update here
});
I'm using this project: http://code4app.net/ios/Perfect-Image-Cropper/51de11576803fa4548000004 for cropping images.
The fact is that is working perfect with photos imported from Library, but not with image from camera.
When selecting image from camera it's showing cropped but stretched.
#interface ImageViewController (){
ImageCropperView *cropper;
UIView *containerView;
UIImageView *finImageView;
UIButton *removeBtn;
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = info[UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
// Media is an image
UIImage *image = info[UIImagePickerControllerOriginalImage];
if (image != nil) {
[self resetCropper];
containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
[self.view addSubview:containerView];
cropper = [[ImageCropperView alloc] initWithFrame:CGRectMake(10, 10, 300, 150)]
[cropper setup];
cropper.image = image;
// Adding the crop button
UIButton *cropBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[cropBtn addTarget:self action:#selector(cropImage:) forControlEvents:UIControlEventTouchUpInside];
[cropBtn setTitle:#"Select" forState:UIControlStateNormal];
cropBtn.frame = CGRectMake(140, 160, 80, 30.0);
[containerView addSubview:cropBtn];
[containerView addSubview:cropper];
}
}
}
// Action when crop button clicked
- (IBAction)cropImage:(id)sender
{
[cropper finishCropping];
UIImage *newImage = cropper.croppedImage;
finImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 150)];
finImageView.image = newImage;
[cropper removeFromSuperview];
[containerView removeFromSuperview];
[self.scrollView insertSubview:finImageView belowSubview:self.actionsTab];
// Adding a button for remove
removeBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
removeBtn.frame = CGRectMake( 0, 0, 22, 22);
[removeBtn setBackgroundImage:[UIImage imageNamed:#"remove"] forState:UIControlStateNormal];
[removeBtn addTarget:self action:#selector(removePhoto:) forControlEvents:UIControlEventTouchUpInside];
[finImageView insertSubview:removeBtn aboveSubview:finImageView];
finImageView.userInteractionEnabled = YES;
}
- (IBAction)removePhoto:(id)sender{
[self resetCropper];
[finImageView removeFromSuperview];
}
- (void)resetCropper
{
[cropper reset];
finImageView.image = nil;
[removeBtn removeFromSuperview];
}
Any help will be very appreciate!
The following works fine in 6.1 but the app crashes in 5.1...
weeklyHeaderViewController =[[WeeklyHeaderViewController alloc] init];
[self.view addSubview:weeklyHeaderViewController.view];
This is an iPad app and the view is 939x31 .xib containing 7 UILabel pairs. The controller'r viewDidLoad method inserts a 4x30 image seperator between the 7 pairs then moves the view into position at the bottom of the screen.
I can trace it through the initWithNibName's so it appears to be alloc ining fine. It crashes with at the addSubview and never gets to the viewDidLoad method.
Is there is something I should here that I am missing with regard to 5.1?
Any suggestions would be appreciated.
Thanks,
John
// WeeklyHeaderViewController.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
CGRect myImageRect = CGRectMake(131, 0, 4, 30);
UIImageView *image = [[UIImageView alloc] initWithFrame:myImageRect];
[image setImage:[UIImage imageNamed:#"weeklyDaySeparator.png"]];
[self.view addSubview:image];
[image release];
myImageRect = CGRectMake(266, 0, 4, 30);
image = [[UIImageView alloc] initWithFrame:myImageRect];
[image setImage:[UIImage imageNamed:#"weeklyDaySeparator.png"]];
self.view addSubview:image];
[image release];
myImageRect = CGRectMake(400, 0, 4, 30);
image = [[UIImageView alloc] initWithFrame:myImageRect];
[image setImage:[UIImage imageNamed:#"weeklyDaySeparator.png"]];
[self.view addSubview:image];
[image release];
myImageRect = CGRectMake(535, 0, 4, 30);
image = [[UIImageView alloc] initWithFrame:myImageRect];
[image setImage:[UIImage imageNamed:#"weeklyDaySeparator.png"]];
[self.view addSubview:image];
[image release];
myImageRect = CGRectMake(670, 0, 4, 30);
image = [[UIImageView alloc] initWithFrame:myImageRect];
[image setImage:[UIImage imageNamed:#"weeklyDaySeparator.png"]];
[self.view addSubview:image];
[image release];
myImageRect = CGRectMake(804, 0, 4, 30);
image = [[UIImageView alloc] initWithFrame:myImageRect];
[image setImage:[UIImage imageNamed:#"weeklyDaySeparator.png"]];
[self.view addSubview:image];
[image release];
self.view.frame = CGRectMake(74, 687-14, self.view.frame.size.width, self.view.frame.size.height);
}
Thanks to iOSBegginer who reminded me to uncheck AutoLayout in my xib.
i am trying to add image which i added in project i use following code for it
UIView *selectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 7, 320, 50)];
[selectionView setBackgroundColor: [UIColor clearColor]];
UIImage *image = [[UIImage alloc]init];
image = [UIImage imageWithContentsOfFile:#"PlaceHolder.png"];
UIImageView* blockView = [[UIImageView alloc] initWithImage:image];
blockView.frame = CGRectMake(0, -5, 45, 45);
if([[[_arrayForTable valueForKey:#"URL"] objectAtIndex:[indexPath row]] isEqualToString:#"No Image"] )
{
[selectionView addSubview:blockView];
}
else
{
NSString *path = [[_arrayForTable valueForKey:#"URL"] objectAtIndex:[indexPath row]];
NSURL *url = [NSURL URLWithString:path];
AsyncImageView *imageView = [[[AsyncImageView alloc] init] autorelease];
imageView.frame = CGRectMake(0, -5, 45, 45);
imageView.imageURL=url;
[selectionView addSubview:imageView];
}
else condition work perfectly they have url of facebook to display images in table view but if condition in not working it going in to if condition but image is not getting displayed plz help
UIImage *image = [UIImage imageNamed:#"PlaceHolder.png"];
UIImageView* blockView = [[UIImageView alloc] initWithImage:image];
/*
imageNamed: is to load the picture which is in bundle. pass in the picture name
imageWithContentsOfFile: need to pass in the whole address of the file
*/
Try
UIImage *image = [UIImage imageNamed:#"yourfile.png"];
and add the image to the imageView and check the image file is aded to your project
So I have an navigation bar I would like to customize and center no matter what I do I either get the image tiling like so.
Here is the code i am using to set the self.navigation.titleView in my UITableControllerView
self.navigationItem.titleView = [self titleView]; // happens in viewDidLoad
- (UIView *)titleView{
UIImage *headerImage = [UIImage imageNamed:#"header#2x"];
UIImageView *containerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, headerImage.size.width, headerImage.size.height)];
return containerView;
}
Thanks.
UIImageView *imgtitle = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 25)];
[imgtitle setContentMode:UIViewContentModeCenter];
self.navigationItem.titleView = imgtitle;
imgtitle.image = [UIImage imageNamed:#"logo.png"];