AutoResizing Image no imageView - iphone

i am using AFOpenFlow in my app, but now the Problem is,
the pictures I use are not the same size because they are from the web!
And how can i autoResizing the image at the view of the iPhone???
Here is my code from this method:
- (void)viewDidLoad {
[super viewDidLoad];
// loading images into the queue
loadImagesOperationQueue = [[NSOperationQueue alloc] init];
NSString *imageName;
for (int i=0; i < 10; i++) {
imageName = [[NSString alloc] initWithFormat:#"cover_%d.jpg", i];
[(AFOpenFlowView *)self.view setImage:[UIImage imageNamed:imageName] forIndex:i];
[imageName release];
NSLog(#"%d is the index",i);
}
[(AFOpenFlowView *)self.view setNumberOfImages:10];
}
Thank you beforehand
Marco

scale your image before set to open flow
change your code in the loop as below:
imageName = [[NSString alloc] initWithFormat:#"cover_%d.jpg", i];
UIImage *oriImage = [UIImage imageNamed:imageName];
[imageName release];
UIImage *requiredImage = [UIImage imageWithCGImage:[oriImage CGImage] scale:768/520.0 orientation:UIImageOrientationUp];
[(AFOpenFlowView *)self.view setImage:requiredImage forIndex:i];
NSLog(#"%d is the index",i);

Related

App getting crash in background image loading iphone

I have scroll view with 60 UIImageView's. Images displaying in these imageviews are from url and url's i get from the webservice. When user scrolls to bottom I call the webservice and get new 60 urls. After getting the url i am utilizing same UIImageView's to display images. Following is my code for displaying images.
for (UIView *viewSel in [scrlView subviews]) {
NSString *strImgUrl = [arrImgURL valueForKey:#"url"];
if ([viewSel isKindOfClass:[UIImageView class]]) {
UIImageView *imgView = (UIImageView *)viewSel;
[imgView setImage:[UIImage imageNamed:#"loading432x520.png"]];
NSMutableArray *arr = [[NSMutableArray alloc] init];
[arr addObject:[NSString stringWithFormat:#"%#",strImgUrl]];
[arr addObject:imgView];
[self performSelectorInBackground:#selector(loadImageInBackground:) withObject:arr];
[arr release];
}
}
- (void) loadImageInBackground:(NSMutableArray *)arr {
NSLog(#"loadImage");
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[arr objectAtIndex:0]]];
UIImage *img = [[UIImage alloc] initWithData:imgData];
if (img != nil) {
[arr addObject:img];
}
else{
[arr addObject:[UIImage imageNamed:#"no-image432x520.png"]];
}
[img release];
[self performSelectorOnMainThread:#selector(assignImageToImageView:) withObject:arr waitUntilDone:YES];
[pool release];
}
- (void) assignImageToImageView:(NSMutableArray *)arr{
NSLog(#"assignImage");
UIImageView *imgView = [arr objectAtIndex:1];
imgView.image = [arr objectAtIndex:2];
}
The code works perfect for first time. But when i get new urls it is working some time or getting crash. I don't know why it is getting crash. I want to stop that crash. If you are not getting to my question then let me know. Please help me for this. Thanks in advance. Valid answer will be appreciated.
Thank you all to give your help full comments to my question. I got the error. I am adding CALayer to uiimageview when allocating memory to uiimageview. I remove that CALayer code and it work perfectly. Nikita's comment help me to find my error.

Received memory warning, while using animations in UIImageview like [smyImageView startAnimating];

//NsMutableArray
//Received memory warning, while using animations in UIImageview like;
self.imageArray = [[NSMutableArray alloc] initWithCapacity:IMAGE_COUNT];
// Build array of images, cycling through image names
if (![self.imageArray count]>0) {
for (int i = 0; i < IMAGE_COUNT; i++){
if (i<10) {
[self.imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"image000%d.jpg", i]]];
}else {
[self.imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"image00%d.jpg", i]]];
}
}
}
/////Received memory warning, while using animations in UIImageview
self.animatedImages.animationImages = [NSArray arrayWithArray:self.imageArray];
[self.imageArray release];
self.imageArray=nil;
// One cycle through all the images takes 1.5 seconds
self.animatedImages.animationDuration = 0.8;
// Repeat forever
self.animatedImages.animationRepeatCount = -1;
[self.animatedImages startAnimating];
Replace
[UIImage imageNamed:[NSString stringWithFormat:#"image000%d.jpg", i]]
with
[UIImage imageWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:#"image000%d.jpg", i] ofType:nil]]];
because the API imageNamed: cache data in memory.

OpenFlow with XML Object

Trying to use OpenFlow API for iPhone SDK.
I need the openflow to load pictures from a (image) link.
This is how it always works with local images:
loadImagesOperationQueue = [[NSOperationQueue alloc] init];
NSString *imageName;
for (int i=0; i < 10; i++) {
imageName = [[NSString alloc] initWithFormat:#"cover_%d.jpg", i];
[(AFOpenFlowView *)self.view setImage:[UIImage imageNamed:imageName] forIndex:i];
[imageName release];
NSLog(#"%d is the index",i);
}
[(AFOpenFlowView *)self.view setNumberOfImages:10];
And I tried this:
- (void)openFlowView:(AFOpenFlowView *)openFlowView requestImageForIndex:(int)index {
NSString *photoUrl = [[oldEntries objectAtIndex: 0] objectForKey: #"still"];
NSURL *photomake = [NSURL URLWithString:photoUrl];
NSLog(#"theConnection: %#",photoUrl);
NSLog(#"theConnection: %#",photomake);
AFGetImageOperation *getImageOperation = [AFGetImageOperation alloc];
// We're getting our images from the Flickr API.
getImageOperation.imageURL = photomake;
[loadImagesOperationQueue addOperation:getImageOperation];
[(AFOpenFlowView *)self.view setNumberOfImages:1];
[getImageOperation release];
}
But it just won't work. Can anyone help me?
Without knowing what 'won't work' it's hard to say, please clarify and supply the actual URL you are creating.
The only line that looks suspect is this one:
AFGetImageOperation *getImageOperation = [AFGetImageOperation alloc];
Should be:
AFGetImageOperation *getImageOperation = [[AFGetImageOperation alloc] init];
It's much easier to use:
NSURL *imageUrl = [[NSURL alloc] initWithString:[#"path/to/the/file.jpg"];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
UIImage *image = [UIImage imageWithData:imageData];
put all the images in an Array
and then you call you first function
for (int i=0; i < 10; i++) {
imageName = [[NSString alloc] initWithFormat:#"cover_%d.jpg", i];
[(AFOpenFlowView *)self.view setImage:**image** forIndex:i];
[imageName release];
NSLog(#"%d is the index",i);
}
[(AFOpenFlowView *)self.view setNumberOfImages:10];

AFOpenFlow touchevent Image

does anybody know a solution that i could implement at this code,
that the pictures could be touched and an event will be started?
- (void)viewDidLoad {
[super viewDidLoad];
// loading images into the queue
loadImagesOperationQueue = [[NSOperationQueue alloc] init];
NSString *imageName;
for (int i=0; i < 10; i++) {
imageName = [[NSString alloc] initWithFormat:#"cover_%d.jpg", i];
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
UIImage *aktuellesImage = imageView.image;
UIImage *scaledImage = [aktuellesImage scaleToSize:CGSizeMake(100.0f, 100.0f)];
[(AFOpenFlowView *)self.view setImage:scaledImage forIndex:i];
[imageName release];
[aktuellesImage release];
}
[(AFOpenFlowView *)self.view setNumberOfImages:10];
}
I hope someone could help me and
sorry for my bad english :)
in AFOpenFlowView.m methods touchesBegan, touchMoves and touchEnded are already overriden, so just create a new delegate method for your usage and adapt these methods.

setting the images in a tableview which i am getting from the server?

I am using the code below to set images in a table which are loaded from the server, but I have an exception thrown (not every time) which is related to a drawing error
- (void) getTheThumnbails:(NSIndexPath *)indexPath{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
GDataEntryYouTubeVideo *entry = [feedArray objectAtIndex:indexPath.row];
NSURL *url = [[NSURL alloc] initWithString:[[[[entry mediaGroup] mediaThumbnails] objectAtIndex:0] URLString]];
NSData *imgData = [[NSData alloc] initWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:imgData];
CGRect newFrame = CGRectMake(0.0, 0.0, 62.0, 62.0);
UIGraphicsBeginImageContext(newFrame.size);
[img drawInRect:newFrame];
UIImage *resizedImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[imgDict setObject:resizedImg forKey:[NSString stringWithFormat:#"%i",indexPath.row]];
[[[myTableView cellForRowAtIndexPath:indexPath] imageView] setImage:resizedImg];
[pool release];
}
Please help me folks, sorting out the mistake i am doing.
Trying to draw a image in a thread is not a good practice, please do not perform any UI operations in thread, they need to be done in the main thread.
well I did the above like
- (void) getTheThumnbails:(NSIndexPath *)indexPath{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
GDataEntryYouTubeVideo *entry = [feedArray objectAtIndex:indexPath.row];
NSURL *url = [[NSURL alloc] initWithString:[[[[entry mediaGroup] mediaThumbnails] objectAtIndex:0] URLString]];
NSData *imgData = [[NSData alloc] initWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:imgData];
[imgDict setObject:img forKey:[NSString stringWithFormat:#"%i",indexPath.row]];
[img release];
[self performSelectorOnMainThread:#selector(setImageInTable:) withObject:indexPath waitUntilDone:NO];
[pool release];
}
- (void)setImageInTable:(NSIndexPath *)indexPath{
UIImage *img = (UIImage *)[imgDict valueForKey:[NSString stringWithFormat:#"%i",indexPath.row]];
CGRect newFrame = CGRectMake(0.0, 0.0, 62.0, 62.0);
UIGraphicsBeginImageContext(newFrame.size);
[img drawInRect:newFrame];
UIImage *resizedImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[imgDict removeObjectForKey:[NSString stringWithFormat:#"%i",indexPath.row]];
[imgDict setObject:resizedImg forKey:[NSString stringWithFormat:#"%i",indexPath.row]];
[[[myTableView cellForRowAtIndexPath:indexPath] imageView] setImage:resizedImg];
}
Hope this helps.
Thanks,
Madhup