Scalling UIImage on the screen size - iphone

I have implemented a small method that imports an picture on the
iPhone screen, but the picture is a little bit to big for the screen.
This is the Method:
- (void)ladeImage {
id path = #"http://172.23.1.63:8080/RestfulJava/pics";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
[self.view addSubview:imgView];
}
But is there any method to scale the picture on the iPhone screen?
Edit1:
Ok thank for your helping, but this method doesn't work, when I switch the iPhone to landscape, her is the code with your tipp:
- (void)ladeImage {
id path = #"http://172.23.1.63:8080/RestfulJava/pics";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
[imgView setContentMode:UIViewContentModeScaleToFill];
imgView.frame = self.view.frame;
[self.view addSubview:imgView];
}
Edit2:
look this is my class:
- (void)viewDidLoad {
[self ladeImage];
[super viewDidLoad];
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
if(toOrientation == UIInterfaceOrientationPortrait || toOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
ImgMainPhoto.frame = CGRectMake(0, 0, 320, 480);
[self PortraitMode];
} else if((toOrientation == UIInterfaceOrientationLandscapeLeft ) || (toOrientation == UIInterfaceOrientationLandscapeRight)) {
ImgMainPhoto.frame = CGRectMake(0, 0, 480, 320);
[self LendscapreMode];
}
}
- (void)ladeImage {
id path = #"http://172.23.1.63:8080/RestfulJava/pics";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
imgView.frame = self.view.frame;
imgView.contentMode = UIViewContentModeScaleToFill;
[self.view addSubview:imgView];
}

you should insert following code before inserting imageview to the view.
[imgView setContentMode:UIViewContentModeScaleToFill]
Also you can set/change from ScaleToFill to AspectFill or as you needed.
and problem will be soved.

Just change the frame to the screen size, i.e. imgView.frame = self.view.frame

look this is my class:
- (void)viewDidLoad {
[self ladeImage];
[super viewDidLoad];
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
if(toOrientation == UIInterfaceOrientationPortrait || toOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
ImgMainPhoto.frame = CGRectMake(0, 0, 320, 480);
[self PortraitMode];
} else if((toOrientation == UIInterfaceOrientationLandscapeLeft ) || (toOrientation == UIInterfaceOrientationLandscapeRight)) {
ImgMainPhoto.frame = CGRectMake(0, 0, 480, 320);
[self LendscapreMode];
}
}
- (void)ladeImage {
id path = #"http://172.23.1.63:8080/RestfulJava/pics";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
imgView.frame = self.view.frame;
imgView.contentMode = UIViewContentModeScaleToFill;
[self.view addSubview:imgView];
}

Related

How we swipe more than 2 images in iOS horizontally?

i write the below code to swipe 2 images. it works fine
but now my task is to swipe more than 2 images horizontally.
and also at the last image if we swipe to the left the first image should appear
how can i achieve this?
-(void)viewDidLoad
{
[super viewDidLoad];
// instantiate gesture recognizer
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(didSwipe:)];
[imageView addGestureRecognizer:rightSwipe];
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(didSwipe:)];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
[imageView addGestureRecognizer:leftSwipe];
//setup images
image1 = [UIImage imageNamed:#"1.png"];
image2 = [UIImage imageNamed:#"2.png"];
imageView.image = image1;
}
-(void)didSwipe : (UISwipeGestureRecognizer *) sender
{
UISwipeGestureRecognizerDirection direction = sender.direction;
switch (direction) {
case UISwipeGestureRecognizerDirectionRight:
imageView.image = image2;
break;
case UISwipeGestureRecognizerDirectionLeft :
imageView.image = image1;
break;
default:
break;
}
}
Use below code and you will get desired output.
In the following code you app will use album photos and automatically sets size of the scrolling as per the number of images in album.
-(void)getAllPictures
{
imageArray=[[NSArray alloc] init];
mutableArray =[[NSMutableArray alloc]init];
NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];
library = [[ALAssetsLibrary alloc] init];
void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != nil) {
if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
[assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];
NSURL *url= (NSURL*) [[result defaultRepresentation]url];
[library assetForURL:url
resultBlock:^(ALAsset *asset) {
[mutableArray addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];
if ([mutableArray count]==count)
{
imageArray=[[NSArray alloc] initWithArray:mutableArray];
[self allPhotosCollected:imageArray];
}
}
failureBlock:^(NSError *error){ NSLog(#"operation was not successfull!"); } ];
}
}
};
NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
[assetGroups addObject:group];
count=[group numberOfAssets];
}
};
assetGroups = [[NSMutableArray alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock:^(NSError *error) {NSLog(#"There is an error");}];
}
-(void)allPhotosCollected:(NSArray*)imgArray
{
CGSize imageSize;
imageSize.width= imageView.bounds.size.width;
imageSize.height = imageView.bounds.size.height;
self.scrollView=[[UIScrollView alloc]init];
self.scrollView.frame = imageView.frame;
self.scrollView.contentSize = CGSizeMake(imageSize.width * imgArray.count, imageSize.height);
self.scrollView.pagingEnabled = YES;
self.scrollView.bounces=YES;
self.scrollView.scrollEnabled = YES;
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
[self.scrollView flashScrollIndicators];
[self.view addSubview:self.scrollView];
CGFloat xPos = 0.0;
for (UIImage *image in imgArray) {
imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(xPos, 0.0, imageSize.width, imageSize.height);
[self.scrollView addSubview:imageView];
xPos += imageSize.width;
// assuming ARC, otherwise release imageView
}
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20,320 , 420)];
[self getAllPictures];
}
Store your multiple images in NSArray.
Now check condition for swipe right if yes than display image from that array.
and if swipe left than display previous image from that array..
Easy to use for loop and increment operator.
I hope you will get idea.

UIScrollView has White Space above SubViews

I am running across a weird issue, which I can't seem to figure out.
Upon adding multiple SubViews to a ScrollView, the ScrollViews keeps on adding a White Space ontop of the regular image
A bit better with more colors:
This is how it should look like:
The Image is perfectly scrollable.
This is the code I'm using to create the scrollview:
-(void)addImagesToScrollView
{
if (self.product != nil)
{
int imageCount = 0;
if (self.product.labelImage != nil)
{
UIImageView *labelImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
labelImage.image = [UIImage imageWithData:self.product.labelImage];
labelImage.contentMode = UIViewContentModeScaleAspectFit;
labelImage.tag = 100;
[self.scrollView addSubview:labelImage];
imageCount += 1;
}
if (self.product.bottleImage != nil)
{
UIImageView *bottleImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
bottleImage.image = [UIImage imageWithData:self.product.bottleImage];
bottleImage.contentMode = UIViewContentModeScaleAspectFit;
bottleImage.tag = 101;
imageCount += 1;
[self.scrollView addSubview:bottleImage];
}
if (self.product.pourImage != nil)
{
UIImageView *pourImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width * 2, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
pourImage.image = [UIImage imageWithData:self.product.pourImage];
pourImage.contentMode = UIViewContentModeScaleAspectFit;
pourImage.tag = 102;
imageCount += 1;
[self.scrollView addSubview:pourImage];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * imageCount, self.scrollView.frame.size.height);
[self.scrollView setContentOffset:CGPointZero animated:NO];
}
}
Edit:
With a BackgroundColor:
use:
bottleImage.layer.color = [UIcolor blueColor].CGColor;
bottleImage.layer.borderWidth = 2.0f;
It will let you see the frame of your UIImageView maybe you are calculating incorrectly the frame
Use below code and you will get desired output. In the following code you app will use album photos and automatically sets size of the scrolling as per the number of images in album. And please modify according to your image repository.
-(void)getAllPictures
{
imageArray=[[NSArray alloc] init];
mutableArray =[[NSMutableArray alloc]init];
NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];
library = [[ALAssetsLibrary alloc] init];
void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != nil) {
if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
[assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];
NSURL *url= (NSURL*) [[result defaultRepresentation]url];
[library assetForURL:url
resultBlock:^(ALAsset *asset) {
[mutableArray addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];
if ([mutableArray count]==count)
{
imageArray=[[NSArray alloc] initWithArray:mutableArray];
[self allPhotosCollected:imageArray];
}
}
failureBlock:^(NSError *error){ NSLog(#"operation was not successfull!"); } ];
}
}
};
NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
[assetGroups addObject:group];
count=[group numberOfAssets];
}
};
assetGroups = [[NSMutableArray alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock:^(NSError *error) {NSLog(#"There is an error");}];
}
-(void)allPhotosCollected:(NSArray*)imgArray
{
CGSize imageSize;
imageSize.width= imageView.bounds.size.width;
imageSize.height = imageView.bounds.size.height;
self.scrollView=[[UIScrollView alloc]init];
self.scrollView.frame = imageView.frame;
self.scrollView.contentSize = CGSizeMake(imageSize.width * imgArray.count, imageSize.height);
self.scrollView.pagingEnabled = YES;
self.scrollView.bounces=YES;
self.scrollView.scrollEnabled = YES;
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
[self.scrollView flashScrollIndicators];
[self.view addSubview:self.scrollView];
CGFloat xPos = 0.0;
for (UIImage *image in imgArray) {
imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(xPos, 0.0, imageSize.width, imageSize.height);
[self.scrollView addSubview:imageView];
xPos += imageSize.width;
// assuming ARC, otherwise release imageView
}
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20,320 , 420)];
[self getAllPictures];
}

Refresh view or remove subviews ios

I'm trying to make a scene where the user can swipe over the screen to browse between posts. The post can be both an image with text or just a note, and the view is altered depending on which one occurs.
The getting process works just perfect. It gets the right post whether i swipe right or left. The problem is that the old view wont disappear and the views are overlapping. This is especially bothering when you go from a note to a photo or vice versa since the sizes ar different...
Here's the code:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#" GETRESULT %# POSTNUMBER %d", getResult, postNumber);
Post *postInfo = [[Post alloc] init];
postInfo = [getResult objectAtIndex:postNumber];
UITextView *postText = [[UITextView alloc] init];
imgView = [[UIImageView alloc] init];
NSString *getImageString = postInfo.attachments;
if(getImageString){
postText = [[UITextView alloc] init];
postText.text = postInfo.noteText;
[postText setFrame:CGRectMake(20, 320, 280, 80)];
NSLog(#"IMG1");
}else {
postText = [[UITextView alloc] init];
postText.text = postInfo.noteText;
[postText setFrame:CGRectMake(20, 10, 280, 240)];
NSLog(#"TEXT1");
}
[self.view addSubview:postText];
SHOWHUD(self.view);
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
NSURL *url = [NSURL URLWithString:getImageString];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
[imgView setFrame:CGRectMake(20, 10, 280, 300)];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
[imgView setImage:img];
dispatch_async(dispatch_get_main_queue(), ^{
[self.view addSubview:imgView];
HIDEHUD(self.view);
});
});
UISwipeGestureRecognizer *recognizerRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeRight:)];
[[self view] addGestureRecognizer:recognizerRight];
UISwipeGestureRecognizer *recognizerLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeLeft:)];
[recognizerLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[[self view] addGestureRecognizer:recognizerLeft];
// Do any additional setup after loading the view.
}
- (void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer{
NSLog(#"SWIPE RIGHT");
if(postNumber > 0){
postNumber--;
Post *postInfo = [[Post alloc] init];
postInfo = [getResult objectAtIndex:postNumber];
UITextView *postText = [[UITextView alloc] init];
imgView = [[UIImageView alloc] init];
NSString *getImageString = postInfo.attachments;
if(getImageString){
[postText removeFromSuperview];
postText = [[UITextView alloc] init];
postText.text = postInfo.noteText;
[postText setFrame:CGRectMake(20, 320, 280, 80)];
NSLog(#"IMG1");
}else {
[postText removeFromSuperview];
postText = [[UITextView alloc] init];
postText.text = postInfo.noteText;
[postText setFrame:CGRectMake(20, 10, 280, 240)];
NSLog(#"TEXT1");
}
[self.view addSubview:postText];
SHOWHUD(self.view);
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
if (getImageString) {
NSURL *url = [NSURL URLWithString:getImageString];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
[imgView setFrame:CGRectMake(20, 10, 280, 300)];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
[imgView setImage:img];
}else {
[imgView removeFromSuperview];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.view addSubview:imgView];
HIDEHUD(self.view);
});
});
}
}
- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer{
NSLog(#"SWIPE LEFT");
if(postNumber < [getResult count] - 1){
postNumber++;
Post *postInfo = [[Post alloc] init];
postInfo = [getResult objectAtIndex:postNumber];
UITextView *postText = [[UITextView alloc] init];
imgView = [[UIImageView alloc] init];
NSString *getImageString = postInfo.attachments;
if(getImageString){
[postText removeFromSuperview];
postText = [[UITextView alloc] init];
postText.text = postInfo.noteText;
[postText setFrame:CGRectMake(20, 320, 280, 80)];
NSLog(#"IMG1");
}else {
[postText removeFromSuperview];
postText = [[UITextView alloc] init];
postText.text = postInfo.noteText;
[postText setFrame:CGRectMake(20, 10, 280, 240)];
NSLog(#"TEXT1");
}
[self.view addSubview:postText];
SHOWHUD(self.view);
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
if(getImageString){
NSURL *url = [NSURL URLWithString:getImageString];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
[imgView setFrame:CGRectMake(20, 10, 280, 300)];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
[imgView setImage:img];
}else {
[imgView removeFromSuperview];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.view addSubview:imgView];
HIDEHUD(self.view);
});
});
}
}
At first i only increased postNumber and called viewDidLoad in the swipe actions. That gave me the exact same result though... This is only one of countless trial and error attempts, and I'm sorry this is among the messier ones...
Would greatly appreciate it if anyone got a solution.
Thanks in advance,
Tom
Did you try this?
for (UIView * view in self.view.subviews) {
if ([view isEqual:postText]||[view isEqual:imgView]) {
[view removeFromSuperview];
}
}
I couldn't remove the variables from the superview because I did it in a new instance of viewDidLoad. I tried to do so before calling viewDidLoad this time and it worked!
Sorry for wasting your time...
Tom

Load qr code from url and display as image view

As the title says I'm trying to take a qr code like this - http://chart.apis.google.com/chart?cht=qr&chs=200x200&chl=044779-A55W6UZD&chld=H|0 and display it as a UIImageview. I've searched but couldn't find an answer anywhere. Please help.
I've tried this but it doesn't seem to work
NSData *receivedData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
UIImage *image = [[UIImage alloc] initWithData:receivedData];
UIImageView * myImageView = [[UIImageView alloc] initWithImage:image];
myImageView.frame = CGRectMake(20, 160, 200, 200);
[self.view addSubview:myImageView];
Try encoding the url like this and then use encodedUrl in the rest of the code:
NSString* encodedUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
You should also remember to release the objects you created, otherwise you leak memory.
NSData* receivedData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:encodedUrl]];
UIImage* image = [[UIImage alloc] initWithData:receivedData];
UIImageView* myImageView = [[UIImageView alloc] initWithImage:image];
myImageView.frame = CGRectMake(20, 160, image.size.width, image.size.height;
[self.view addSubview:myImageView];
[receivedData release];
[image release];
[myImageView release];

I can't see any picture. Why?

I can't see any picture. Why?
UIImage *photoImg = [UIImage imageNamed:#"http://blog.leadcritic.com/wp-content/uploads/2011/02/favorite_animal_picture.jpg"];
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:photoImg];
[self.view addSubview:imageView2];
Because if you use the code:
[UIImage imageNamed:#"mypicture.png"];
The image should be on the iPhone and not loaded from a URL. From a URL you need to use the following:
NSString path = #"http://blog.leadcritic.com/wp-content/uploads/2011/02/favorite_animal_picture.jpg";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *photoImg = [[UIImage alloc] initWithData:data cache:NO];
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:photoImg];
You haven't set the frame of imageView2
imageView2.frame = CGRectMake( 0, 0, 100, 100 ); // Or whatever.