UIImage imageWithData - iphone

I have a UITable and when I click on a row I load a set of images.
I create the images with
UIImage *image = [UIImage imageWithData:data];
The problem is that ever time I click on the same row, the images are re-allocated into memory, like if no cache it is used and no memory is freed, so the allocated memory keep increasing. (I see this using the Inspector on both the device and the emulator)
I am releasing everything and also, with clang, everything looks fine.
The point is that after a while my app crashes.
Any suggestion or idea?
EDIT:
Here is the portion of code I think is generating the issue:
UIView *main = [[[UIView alloc] initWithFrame:rectScrollView] autorelease];
for (int i=0; i<[contentArray count]; i++) {
float targetWidth = rectScrollView.size.width;
float targetHeight = rectScrollView.size.height;
Link *tmp_link = [contentArray objectAtIndex:i];
AsyncImageView* asyncImage = nil;
if(tmp_link!=nil){
CGRect imageFrame = CGRectMake(targetWidth*i, 0, targetWidth, targetHeight);
//asyncImage = [[[AsyncImageView alloc] initWithFrame:imageFrame ] autorelease];
asyncImage = [[AsyncImageView alloc] initWithFrame:imageFrame ];
asyncImage.contentMode = UIViewContentModeScaleAspectFit;
NSURL* url = [NSURL URLWithString:[[IMG_SERVER_URL stringByAppendingString: tmp_link.url] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
[asyncImage loadImageFromURL:url];
[self.scrollView addSubview:(UIImageView *)asyncImage];
[asyncImage release];
}
if ([tmp_link.caption length] > 0 && asyncImage!=nil) {
float marginTop = 25;
UILabel *caption = [[UILabel alloc] init];
caption.text = tmp_link.caption;
caption.textAlignment = UITextAlignmentCenter;
caption.textColor = [UIColor whiteColor];
caption.font = [UIFont systemFontOfSize:12];
caption.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.4];
CGRect captionFrame = CGRectMake((i * targetWidth), marginTop+10, targetWidth, 20.0f);
caption.frame = captionFrame;
[self.scrollView addSubview:caption];
[caption release];
}
//[asyncImage release];
}
[main addSubview:scrollView];
[scrollView release];
if (pageControlEnabledTop) {
rectPageControl = CGRectMake(0, 5, scrollWidth, 15);
}
else if (pageControlEnabledBottom) {
//rectPageControl = CGRectMake(0, (scrollHeight - 25), scrollWidth, 15);
rectPageControl = CGRectMake(0, (scrollHeight), scrollWidth, 15);
}
if (pageControlEnabledTop || pageControlEnabledBottom) {
//pageControl = [[[UIPageControl alloc] initWithFrame:rectPageControl] autorelease];
pageControl = [[UIPageControl alloc] initWithFrame:rectPageControl];
pageControl.numberOfPages = [contentArray count];
pageControl.currentPage = page;
[main addSubview:pageControl];
[pageControl release];
}
if (pageControlEnabledTop || pageControlEnabledBottom || rememberPosition) self.scrollView.delegate = self;
//if (margin) [margin release];
return (UIScrollView *)main;
And it is used like this:
reviewImagesImage = [[IGUIScrollViewImage alloc] init];
if([currentReview.linkScreenshots count]>0){
//reviewImages
reviewImages = [[UIView alloc] init];
//placing the images
CGRect frameImages = reviewImages.frame;
frameImages.origin.x = 10;
frameImages.origin.y = (infoView.frame.origin.y + infoView.frame.size.height + 10.0f);
frameImages.size.width = 300;
frameImages.size.height = 350.0f;
reviewImages.frame = frameImages;
reviewImages.backgroundColor = [UIColor clearColor];
[reviewImagesImage setContentArray:currentReview.linkScreenshots];
[reviewImagesImage setWidth:reviewImages.frame.size.width andHeight: reviewImages.frame.size.height];
[reviewImagesImage enablePageControlOnBottom];
[reviewImagesImage setBackGroudColor: [UIColor clearColor]];
UIScrollView *tmp_sv = [reviewImagesImage getWithPosition:0];
[reviewImages addSubview:tmp_sv];
[self.view addSubview:reviewImages];
[reviewImages release];
}
I have noticed that if I try to release reviewImagesImage my app crashes :/

Of course no cache is used because new UIImage will be created from the data every time. If memory usage significantly increased like you have said, it is most likely that it is our fault that unintentionally keep the reference retained somewhere.

Once you've set your image with imageWithData, save the image to the filepath in the app bundle.

Related

How to change the image on UIScrollView

I have a scroll view with multiple images. The images are scrollable.
I am using the following code:
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scrollView.pagingEnabled = YES;
scrollView.delegate = self;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
NSInteger numberOfViews = [arrayImage count];
for (int i = 0; i < numberOfViews; i++)
{
CGFloat yOrigin = i * scrollView.frame.size.width;
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(yOrigin +30, 0, self.view.frame.size.width-60 , self.view.frame.size.height)];
imageView.image = [UIImage imageNamed:[arrayImage objectAtIndex:i]];
[scrollView addSubview:imageView];
[imageView release];
}
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * numberOfViews,0);
[self.view addSubview:scrollView];
[scrollView release]
The application will work with iPad orientations, but the images are not properly scrolled in the landscape mode.
It works fine with portrait mode, but the images merge in landscape mode.
If somebody has any idea, please share....
Try this one...
scrollView.delegate = self;
scrollView.scrollEnabled = YES;
int scrollWidth = 120;
scrollView.contentSize = CGSizeMake(scrollWidth,80);
int xOffset = 0;
imageView.image = [UIImage imageNamed:[imagesName objectAtIndex:0]];
for(int index=0; index < [imagesName count]; index++)
{
UIImageView *img = [[UIImageView alloc] init];
img.bounds = CGRectMake(10, 10, 50, 50);
img.frame = CGRectMake(5+xOffset, 0, 50, 50);
NSLog(#"image: %#",[imagesName objectAtIndex:index]);
img.image = [UIImage imageNamed:[imagesName objectAtIndex:index]];
[images insertObject:img atIndex:index];
scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,110);
[scrollView addSubview:[images objectAtIndex:index]];
xOffset += 70;
}
Set this...
imagesName = [[NSArray alloc]initWithObjects:#"image1.jpg",#"image2.jpg",#"image3.jpg",#"image4.jpg",#"image5.jpg",#"image6.png",#"image7.png",#"image9.png",nil];
images = [[NSMutableArray alloc]init];
You have to set the image frame for that purpose everytime the interface orientation changes or you have to redraw the images again after the change in orientation

Thumbnail grid of images

How does one create a dynamic thumbnail grid?
How is a grid created with thumbnails of images like the photo app on iphone?
How are these spaces arranged dynamically? e.g. adding and removing thumbnails.
this is my simple grid view app
- (void)viewDidAppear:(BOOL)animated {
[[Grid_ViewAppDelegate sharedAppDelegate] hideLoadingView];
temp = temp+1;
if (temp ==1){
NSLog(#"temp:%d",temp);
int n = 20; //Numbers of array;
NSArray *t = [[NSArray alloc] initWithObjects:#"calpie.gif",#"chrysler_electric.png",#"chrysler_fiat_cap.gif",#"cleanup.gif",#"ecylindri.gif",#"elect08.png",#"globe.gif",#"heat.gif",#"insur.gif"
,#"jobs.gif",#"office.gif",#"opec1.gif",#"orng_cap.gif",#"poll.gif",#"pollen.gif",#"purbar.gif",#"robinson.gif",#"robslink_cap.gif",#"scot_cap.gif",#"shoes.gif",nil];
myScrollView.contentSize = CGSizeMake(320, 460+n*2);
myScrollView.maximumZoomScale = 4.0;
myScrollView.minimumZoomScale = 1;
myScrollView.clipsToBounds = YES;
myScrollView.delegate = self;
NSString *mxiURL = #"http://meta-x.com/biflash/images/";
int i =0,i1=0;
while(i<n){
int yy = 4 +i1*79;
for(int j=0; j<4;j++){
if (i>=n) break;
CGRect rect;
rect = CGRectMake(4+79*j, yy, 75, 75);
UIButton *button=[[UIButton alloc] initWithFrame:rect];
[button setFrame:rect];
NSString *nn = [mxiURL stringByAppendingString:[t objectAtIndex:i]];
UIImage *buttonImageNormal=[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: nn]]];
[button setBackgroundImage:buttonImageNormal forState:UIControlStateNormal];
button.tag =i;
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside ];
//[self.view addSubview:button];
[myScrollView addSubview:button];
//[buttonImageNormal release];
[button release];
i++;
//
}
i1 = i1+1;
//i=i+4;
}
}
}
in AppDelegate
+ (Grid_ViewAppDelegate *)sharedAppDelegate
{
return (Grid_ViewAppDelegate *)[UIApplication sharedApplication].delegate;
}
- (void)showLoadingView
{
if (loadingView == nil)
{
loadingView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)];
loadingView.opaque = NO;
loadingView.backgroundColor = [UIColor grayColor];
loadingView.alpha = 0.5;
UIActivityIndicatorView *spinningWheel = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(142.0, 222.0, 37.0, 37.0)];
[spinningWheel startAnimating];
spinningWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[loadingView addSubview:spinningWheel];
[spinningWheel release];
CGRect label = CGRectMake(142.0f, 255.0f, 71.0f, 20.0f);
lblLoading = [[UILabel alloc] initWithFrame:label];
lblLoading.textColor = [UIColor whiteColor];
lblLoading.font = [UIFont boldSystemFontOfSize:14];
// UILabel
lblLoading.backgroundColor =[UIColor clearColor];
[lblLoading setText:#"Loading..."];
//[lblLoading setTextAlignment:UITextAlignmentCenter];
[loadingView addSubview:lblLoading];
}
[window addSubview:loadingView];
}
- (void)hideLoadingView
{
[loadingView removeFromSuperview];
}
definitely if u apply this logic. u get succeed

How to catch event inside uiscrollview on image

Hi I have populated uiscrollview with images (code is bellow) but I don't know how to add some event to images inside. I want to double tap on image inside scrollview and get some event. Any direction how to achieve this?
arrayOfImages = [[NSMutableArray alloc] init];
NSString *img;
for (img in imgArray)
{
[arrayOfImages addObject:[UIImage imageNamed:img]];
}
NSLog(#"Array initialization complete...");
scrollView = [[UIScrollView alloc] init];
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
scrollView.directionalLockEnabled = YES;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.delegate = self;
scrollView.backgroundColor = [UIColor blueColor];
scrollView.autoresizesSubviews = YES;
scrollView.frame = CGRectMake(0, 0, 320, 29);
[self.view addSubview:scrollView];
NSLog(#"Scroll View initialization setup complete...");
UIImage *imageToAdd;
int x = 0;
int y = 0;
for (imageToAdd in arrayOfImages)
{
UIImageView *temp = [[UIImageView alloc] initWithImage:imageToAdd];
temp.frame = CGRectMake(x, y, 29, 29);
x += 29;
[scrollView addSubview:temp];
}
NSLog(#"Adding images to outlet complete...");
Add a UITapGestureRecognizer to the UIImageView(s).
Look at this guide for more info.

UIPageControl is not Displayed

I use the following to display scrollview and pagecontrol
scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 179)];
pageControl=[[UIPageControl alloc] initWithFrame:CGRectMake(0, 179, 320, 20)];
scrollView.delegate = self;
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
int index=[[self._parameters objectForKey:#"index"] intValue];
NSString *imageNamePrefix=[[activeAppIdList objectAtIndex:index] objectForKey:#"AppID"];
int noOfScreenShot=[[[activeAppIdList objectAtIndex:index] objectForKey:#"NoOfScreenShot"] intValue];
CGFloat cx = 0;
for (int j=1;j<=noOfScreenShot ; j++) {
UIImage *image =[[UIImage alloc] init];
image= [self loadImage:[[NSString alloc]initWithFormat:#"%#_%d.jpg",imageNamePrefix,j]];
if (image == nil) {
NSString *urlString = [[NSString alloc]initWithFormat:#"http://localhost/MoreApp/images/%#_%d.jpg",imageNamePrefix,j];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
UIImage *temp=[[UIImage alloc]initWithData:imageData] ;
[self saveImage:temp withName:[[NSString alloc]initWithFormat:#"%#_%d.jpg",imageNamePrefix,j]];
image = temp;
[temp release];
[urlString release];
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[image release];
imageView.frame = CGRectMake(cx,0,scrollView.frame.size.width,scrollView.frame.size.height);
[scrollView addSubview:imageView];
[imageView release];
cx += scrollView.frame.size.width;
}
NSLog(#"No Of pages..%d",noOfScreenShot);
pageControl.numberOfPages = noOfScreenShot;
pageControl.currentPage = 0;
scrollView.contentSize = CGSizeMake(cx,scrollView.frame.size.height);
NSLog(#"Width:%f",scrollView.frame.size.width);
[ refToSelf.instructionView addSubview:scrollView];
[ refToSelf.instructionView addSubview:pageControl];
[scrollView release];
[pageControl release];
scrollview is ok but pagecontroll didn't showed...
can help??
If the page control's and container's background color is the same (by default white) page control won't be visible.
For anyone who finds this, another silly mistake you can make is to not set the numberOfPages attribute on the UIPageControl, which will also cause it to not display.
My mistake was equally annoying - developing with storyboard in 3.5 inch view, and built on 4 inch device on simulator so it was offscreen. Silly!

Rotate UIPickerView, Possible?

Is there a way to rotate an UIPickerView?
//example code placed in view did Load
[super viewDidLoad];
myPicker.delegate = self;
myPicker.dataSource = self;
myPicker.showsSelectionIndicator =YES;
myPicker.backgroundColor = [UIColor blackColor];
CGAffineTransform rotate = CGAffineTransformMakeRotation(3.14/2);
//original settings
//rotate = CGAffineTransformScale(rotate, 0.1, 0.8);
rotate = CGAffineTransformScale(rotate, 0.2, 1.65);
[self.myPicker setTransform:rotate];
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:#"155", #"165", #"175",
#"185", #"195", #"205", #"215", nil];
self.pickerData = array;
[array release];
NSInteger TOTALITEM = [pickerData count];
UILabel *theview[TOTALITEM-1]; // + 1 for the nil
for (int i=1;i<=TOTALITEM-1;i++) {
theview[i] = [[UILabel alloc] init];
theview[i].text = [NSString stringWithFormat:#"%d",i];
theview[i].textColor = [UIColor blackColor];
theview[i].frame = CGRectMake(0,0, 25, 25);
theview[i].backgroundColor = [UIColor clearColor];
theview[i].textAlignment = UITextAlignmentCenter;
theview[i].shadowColor = [UIColor whiteColor];
theview[i].shadowOffset = CGSizeMake(-1,-1);
theview[i].adjustsFontSizeToFitWidth = NO;
UIFont *myFont = [UIFont fontWithName:#"Helvetica" size:30];
[theview[i] setFont:myFont];
}
CGAffineTransform rotateItem = CGAffineTransformMakeRotation(-3.14/2);
rotateItem = CGAffineTransformScale(rotateItem, .5, 5);
for (int j=1;j<=TOTALITEM-1;j++) {
theview[j].transform = rotateItem;
}
pickerData = [[NSMutableArray alloc] init];
for (int j=1;j<=TOTALITEM-1;j++) {
[pickerData addObject:theview[j]];
}
You can rotate a UIPickerView by applying a transform to a view containing it, much like I suggest in my answer here for scaling the picker.
Are you looking for - (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated?
Or do you mean that you want to rotate the whole control 90 degrees, thus having wheels that spin to the left and right?