Show multiple photos in scroller - iphone

In the app, I need to show no. of images (count- unknown) in scroller. I put all the images in scroller directly. But start receiving the memory warning level 2. Please suggest me how to do so. Is there any other way to do that?
You can also refer to following link to have better understanding of the problem.
https://www.dropbox.com/sh/h0vwnhhx1acfcb5/W2TQ638udh
-(void) getPortraitScrollReady
{
UIImageView *imageView;
NSString *imageName;
int x=0;
for (NSNumber *numberObject in tagList)
{
imageName = [[NSString alloc] initWithFormat: #"%#", [self getImage:[numberObject intValue]]];
imageView = [[UIImageView alloc ] initWithFrame: CGRectMake(x, 0, 320, 320)];
imageView.image = [self thumbWithSideOfLength:320 pathForMain:imageName]; // a function that returns an image;
[scrollPortrait addSubview:imageView];
[imageName release];
[imageView release];
x+=320;
}
scrollPortrait.contentSize = CGSizeMake(x, 320);
int pageNo =[self indexofObject:imageNo inArray:tagList];
[scrollPortrait setContentOffset:CGPointMake(pageNo*320, 0) animated:NO];
}

Keep all images in an Array/MutableArray as per convenience.
Take Scrollview, In scroll view take 5-UIImageViews to display images from array. Match the length of that UIScrollView with 5-UIImageViews.
When scrolling is done in any of the direction, then 'release' the UIImageView in opposite direction & 'allocate' a new UIImageVIew in scrolled direction.
This is what I did in my project.
.h file contents
IBOutlet UIScrollView *scrlView;
UIImageView *imgView1;
UIImageView *imgView2;
UIImageView *imgView3;
UIImageView *imgView4;
UIImageView *imgView5;
NSMutableArray *imgGallery;
NSInteger mCount;
NSInteger centerImg;
CGFloat imgFrameHeight;
CGFloat imgView1X;
CGFloat imgView2X;
CGFloat imgView3X;
CGFloat imgView4X;
CGFloat imgView5X;
CGFloat previousOffsetX;
CGFloat currentOffsetX;
.m file contents
- (void)photoGallery
{
imgGallery = [[NSMutableArray alloc] initWithCapacity:10];
[imgGallery addObject:[UIImage imageNamed:#"1.png"]];
[imgGallery addObject:[UIImage imageNamed:#"2.png"]];
[imgGallery addObject:[UIImage imageNamed:#"3.png"]];
[imgGallery addObject:[UIImage imageNamed:#"4.png"]];
[imgGallery addObject:[UIImage imageNamed:#"5.png"]];
[imgGallery addObject:[UIImage imageNamed:#"6.png"]];
[imgGallery addObject:[UIImage imageNamed:#"7.png"]];
[imgGallery addObject:[UIImage imageNamed:#"8.png"]];
[imgGallery addObject:[UIImage imageNamed:#"9.png"]];
}
- (void)photoScroller
{
CGFloat appFrameHeight = self.view.frame.size.height;
CGFloat navFrameHeight = self.navigationController.navigationBar.frame.size.height;
CGFloat tabFrameHeight = self.tabBarController.tabBar.frame.size.height;
imgFrameHeight = appFrameHeight - (navFrameHeight+tabFrameHeight);
mCount = [imgGallery count];
[scrlView setContentSize:CGSizeMake(320 * mCount, imgFrameHeight)];
CGFloat offsetX = ((320 * mCount)/2) - 160;
[scrlView setContentOffset:CGPointMake(offsetX, 0)];
imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(offsetX - (320*2), 0, 320, imgFrameHeight)];
imgView2 = [[UIImageView alloc] initWithFrame:CGRectMake(offsetX - 320, 0, 320, imgFrameHeight)];
imgView3 = [[UIImageView alloc] initWithFrame:CGRectMake(offsetX, 0, 320, imgFrameHeight)];
imgView4 = [[UIImageView alloc] initWithFrame:CGRectMake(offsetX + 320, 0, 320, imgFrameHeight)];
imgView5 = [[UIImageView alloc] initWithFrame:CGRectMake(offsetX + (320*2), 0, 320, imgFrameHeight)];
int center = mCount/2;
int tmp = center * 2;
int remainder = mCount - tmp;
if (remainder != 0) {
centerImg = center;
} else {
centerImg = center - remainder;
}
imgView1.image = [imgGallery objectAtIndex:(centerImg-2)];
imgView2.image = [imgGallery objectAtIndex:(centerImg-1)];
imgView3.image = [imgGallery objectAtIndex:centerImg];
imgView4.image = [imgGallery objectAtIndex:(centerImg+1)];
imgView5.image = [imgGallery objectAtIndex:(centerImg+2)];
[scrlView addSubview:imgView1];
[scrlView addSubview:imgView2];
[scrlView addSubview:imgView3];
[scrlView addSubview:imgView4];
[scrlView addSubview:imgView5];
}
Below is the code where UIImageViews are adjusted
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
imgView1X = imgView1.frame.origin.x;
imgView2X = imgView2.frame.origin.x;
imgView3X = imgView3.frame.origin.x;
imgView4X = imgView4.frame.origin.x;
imgView5X = imgView5.frame.origin.x;
CGPoint previousOffset = [scrlView contentOffset];
previousOffsetX = previousOffset.x;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//get the current offset
CGPoint currentOffset = [scrollView contentOffset];
currentOffsetX = currentOffset.x;
//NSLog(NSStringFromCGPoint(currentOffset));
//calculate offset X of RIGHT ImageView to be shifted
CGFloat identifyRightIV = previousOffsetX +640;
//calcualte new LEFT offset X for shifting the previously calcualted RIGHT ImageView
CGFloat newLX = identifyRightIV - (320*5);
//calculate offset X of LEFT ImageView to be shifted
CGFloat identifyLeftIV = previousOffsetX -640;
//calcualte new RIGHT offset X for shifting the previously calcualted LEFT ImageView
CGFloat newRX = identifyLeftIV + (320*5);
//index of new LEFT Image in an array imgGallery
int newImageL = newLX / 320;
//index of new RIGHT Image in an array imgGallery
int newImageR = newRX / 320;
//if scrolled to left
if (newLX >= 0) // Is shifting is necessary? i.e. if new LEFT offsetX is greater than lowest offsetX(0) of scrollview
{
if (currentOffsetX == (previousOffsetX-320))
{
if (imgView1X == identifyRightIV) {
imgView1.frame = CGRectMake(newLX, 0, 320, imgFrameHeight);
imgView1.image = [imgGallery objectAtIndex:newImageL];
}
else if (imgView2X == identifyRightIV) {
imgView2.frame = CGRectMake(newLX, 0, 320, imgFrameHeight);
imgView2.image = [imgGallery objectAtIndex:newImageL];
}
else if (imgView3X == identifyRightIV) {
imgView3.frame = CGRectMake(newLX, 0, 320, imgFrameHeight);
imgView3.image = [imgGallery objectAtIndex:newImageL];
}
else if (imgView4X == identifyRightIV) {
imgView4.frame = CGRectMake(newLX, 0, 320, imgFrameHeight);
imgView4.image = [imgGallery objectAtIndex:newImageL];
}
else if (imgView5X == identifyRightIV) {
imgView5.frame = CGRectMake(newLX, 0, 320, imgFrameHeight);
imgView5.image = [imgGallery objectAtIndex:newImageL];
}
}
}
//if scrolled to right
if (newRX < (mCount*320)) // Is shifting is necessary? i.e. if new RIGHT offsetX is less than highest offsetX of scrollview
{
if (currentOffsetX == (previousOffsetX+320))
{
if (imgView1X == identifyLeftIV) {
imgView1.frame = CGRectMake(newRX, 0, 320, imgFrameHeight);
imgView1.image = [imgGallery objectAtIndex:newImageR];
}
else if (imgView2X == identifyLeftIV) {
imgView2.frame = CGRectMake(newRX, 0, 320, imgFrameHeight);
imgView2.image = [imgGallery objectAtIndex:newImageR];
}
else if (imgView3X == identifyLeftIV) {
imgView3.frame = CGRectMake(newRX, 0, 320, imgFrameHeight);
imgView3.image = [imgGallery objectAtIndex:newImageR];
}
else if (imgView4X == identifyLeftIV) {
imgView4.frame = CGRectMake(newRX, 0, 320, imgFrameHeight);
imgView4.image = [imgGallery objectAtIndex:newImageR];
}
else if (imgView5X == identifyLeftIV) {
imgView5.frame = CGRectMake(newRX, 0, 320, imgFrameHeight);
imgView5.image = [imgGallery objectAtIndex:newImageR];
}
}
}
}
Due to this in entire life cycle only 5-UIImageViews will be in App & all images will be safe in array.

try this code for 1st question:
scrl=[[UIScrollView alloc]initWithFrame:CGRectMake(10, 92, 300, 370)];
//scrl.backgroundColor=[UIColor blackColor];
scrl.delegate=self;
scrl.showsHorizontalScrollIndicator=NO;
scrl.showsVerticalScrollIndicator=NO;
scrl.pagingEnabled=YES;
scrl.layer.cornerRadius = 5.0f;
scrl.layer.masksToBounds = YES;
scrl.layer.borderWidth = 5;
scrl.layer.borderColor = [UIColor blackColor].CGColor;
int i, x=0,y=0;
for (i=0; i<[imageName count]; i++)
{
imagBack=[[UIImageView alloc] initWithFrame:CGRectMake(x,y,300, 370)];
imagBack.image=[UIImage imageNamed:[imageName objectAtIndex:i]];
[scrl addSubview:imagBack];
x =x+300;
}
scrl.contentSize=CGSizeMake(x,370);
[self.view addSubview:scrl];
-> 2nd issue,you use the image to be compressed (.png format) image is ,try this:
[self imageWithImage:image CovertToSize:CGSizeMake(46, 44)];
NSData* imageData = UIImageJPEGRepresentation(image, 1.0f);//10 % quality compressed
call this method when save the image:
-(UIImage *)imageWithImage:(UIImage *)image CovertToSize:(CGSize)size {
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
destImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return destImage;
}

Related

How can I add a white space between image?

I write a code to loop for creating multiple image on UIImageView inside scrollview. i did something like this:
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(38, 9, 90, 280)];
int numberOfImages = 9;
CGFloat currentY = 0.5f;
for (int i=1; i <= numberOfImages; i++) {
NSString *imageName = [NSString stringWithFormat:#"img0%d.jpg",i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(2, 15, 87, imageView.frame.size.height - 20);
CGRect rect = imageView.frame;
rect.origin.y = currentY;
imageView.frame = rect;
currentY += imageView.frame.size.height;
[scrollView addSubview:imageView];
}
scrollView.contentSize = CGSizeMake(87,currentY);
[self.view addSubview:scrollView];
Now, i need to add a space between each image, how can i do that?
Add a bit more to currentY?
currentY += imageView.frame.size.height + 12.0f;

Save and load a UIScrollView position

Im creating an app that uses a UIScrollView that scrolls through images and a segmented control that loads a new view and allows a new layer of images (bit like Photoshop layering) - For some reason when I click back on the layer, it remembers the position but creates a new layer.... How can I stop this from happening, so once a layer has been selected and an image chosen, when the user returns it goes to the last loaded position?
Also, as a side thing... I cannot use my gesture recognition or UIAlertView when the UIScrollView is loaded? Does anyone know why?
Here is my code.... And thanks in advance:)
- layerControl:(NSInteger)index
{
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger viewCount = 15;
for (int i = 0; i < viewCount; i++) {
CGFloat yOrigin = i * self.view.frame.size.width;
UIImageView *filterViewOverlay = [[UIImageView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
[filterViewOverlay setImage:[filterManager objectAtIndex:i]];
[scroll addSubview:filterViewOverlay];
[filterViewOverlay release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * viewCount, self.view.frame.size.height);
[self.view addSubview:scroll];
[scroll release];
switch (index)
{
case 0: scroll = self.scroll1;
case 1: scroll = self.scroll2;
case 2: scroll = self.scroll3;
case 4: scroll = self.scroll4;
case 5: scroll = self.scroll5;
}
/* if (scroll == nil)
{
[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger viewCount = 15;
for (int i = 0; i < viewCount; i++) {
CGFloat yOrigin = i * self.view.frame.size.width;
UIImageView *filterViewOverlay = [[UIImageView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
[filterViewOverlay setImage:[filterManager objectAtIndex:i]];
[scroll addSubview:filterViewOverlay];
//[filterViewOverlay release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * viewCount, self.view.frame.size.height);
[self.view addSubview:scroll];
[scroll autorelease]; */
}
}
//Method to add a filter to the page
- (void)addFilter:(id)sender
{
UISegmentedControl *filterController = (UISegmentedControl *)sender;
[self layerControl:[filterController selectedSegmentIndex]];
}
- (UIImage*)saveImages
{
UIImage *image = [UIImage imageNamed:#"UrbanPhoto##1"];
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Try saving the value of UIScrollView.contentOffset before the user action, and then restoring that value after the action is completed.

iphone: how to add text above and below the image in the scrollvew?

I would like to add a page number above the image such as "1 of 50" and description of the image below the image in the scrollview.
I have looked at this LINK but still couldn't figure out how to make it happen.
here is my sample code of the images scrollview. Im trying to find a sample that can add text and scroll with the images
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
NSString *imageName = [NSString stringWithFormat:#"images%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;
imageView.tag = i; // tag our images for later use when we place them in serial fashion
[scrollView1 addSubview:imageView];
[imageView release];
}
UPDATE:
const CGFloat kScrollObjHeight = 320;
const CGFloat kScrollObjWidth = 280.0;
const NSUInteger kNumImages = 50;
- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
- (void)viewDidLoad {
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
// 1. setup the scrollview for multiple images and add it to the view controller
//
// note: the following can be done in Interface Builder, but we show this in code for clarity
[scrollView1 setBackgroundColor:[UIColor blackColor]];
[scrollView1 setCanCancelContentTouches:NO];
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView1.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
scrollView1.scrollEnabled = YES;
// pagingEnabled property default is NO, if set the scroller will stop or snap at each photo
// if you want free-flowing scroll, don't set this property.
scrollView1.pagingEnabled = YES;
// load all the images from our bundle and add them to the scroll view
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
NSString *imageName = [NSString stringWithFormat:#"creative%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;
imageView.tag = i; // tag our images for later use when we place them in serial fashion
[scrollView1 addSubview:imageView];
[imageView release];
}
[self layoutScrollImages];
I wanted to put this but unable to find the correct position or right offset to display at the top
UILabel * topLabel = [[UILabel alloc] init];
topLabel.text = [NSString stringWithFormat:#"%d of %d", i, kNumImages];
rect.origin.x = offset;
rect.size.height = 30; // however large you need the label to be
topLabel.frame = rect;
offset += 30;
I would think something like this would work:
float offset = 0;
for (NSUInteger i = 1; i <= kNumImages; i++)
{
// load up the image
NSString *imageName = [NSString stringWithFormat:#"images%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// image.size is important here and used to determine placement
CGRect rect = imageView.frame;
rect.size = image.size;
// create top label, just basic will need to configure other settings like font
UILabel * topLabel = [[UILabel alloc] init];
topLabel.text = [NSString stringWithFormat:#"%d of %d", i, kNumImages];
rect.origin.x = offset;
rect.size.height = 30; // however large you need the label to be
topLabel.frame = rect;
offset += 30; // adding the label height
// set image frame since we now know the location below top label
rect.size = image.size;
rect.origin.x += offset;
imageView.frame = rect;
imageView.tag = i;
offset += image.size.height; // adding image height
// add bottom label below image
UILabel * bottomLabel = [[UILabel alloc] init];
bottomLabel.text = imageName; // just a sample description
rect.origin.x += offset;
rect.size.height = 30; // however large you need the label to be
bottomLabel.frame = rect;
offset += 30; // adding the label height
[scrollView1 addSubview:topLabel];
[scrollView1 addSubview:imageView];
[scrollView1 addSubview:bottomLabel];
[imageView release];
[topLabel release];
[bottomLabel release];
offset += 20; // arbitrary spacing between images
}

PDF and zoom - CATiledLayer

I have a problem, I'm not a programmer but I try to do some app for fun.
I try to create a pdf viewer, I create a UIView class like this:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:(CGRect)frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
NSString *pathToPdfDoc = [[NSBundle mainBundle] pathForResource:#"myPDF" ofType:#"pdf"];
NSURL *pdfUrl = [NSURL fileURLWithPath:pathToPdfDoc];
document = CGPDFDocumentCreateWithURL((CFURLRef)pdfUrl);
currentPage = 1;
}
return self;
}
-(void)drawRect:(CGRect)inRect{
if(document) {
CGPDFPageRef page = CGPDFDocumentGetPage(document, currentPage);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGContextTranslateCTM(ctx, 0.0, [self bounds].size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, [self bounds], 0, true));
CGContextDrawPDFPage(ctx, page);
CGContextRestoreGState(ctx);
}
}
-(void)increasePageNumber {
size_t pageCount = CGPDFDocumentGetNumberOfPages(document);
if (currentPage == pageCount) {
// do nothing
}
else {
currentPage++;
[self setNeedsDisplay];
}
}
-(void)decreasePageNumber {
if (currentPage == 1) {
// do nothing
}
else {
currentPage--;
[self setNeedsDisplay];
}
}
now I try to add zoom but without luck, fro zoom I create a layer:
-(void)viewDidLoad {
CGRect frame;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
frame = CGRectMake(0, 0, 960, 1024);
}
else{
frame = CGRectMake(0, 0, 320, 460);
}
tiledLayer = [CATiledLayer layer];
tiledLayer.delegate = self;
tiledLayer.tileSize = CGSizeMake(1024, 1024);
tiledLayer.levelsOfDetail = 200;
tiledLayer.levelsOfDetailBias = 200;
tiledLayer.frame = frame;
myPDFView = [[viewPDF alloc] initWithFrame:frame];
//[self.view addSubview:myPDFView];
[myPDFView.layer addSublayer:tiledLayer];
CGRect viewFrame = self.view.frame;
viewFrame.origin = CGPointZero;
scrollView = [[UIScrollView alloc] initWithFrame:viewFrame];
scrollView.delegate = self;
scrollView.contentSize = frame.size;
scrollView.maximumZoomScale = 5;
[scrollView addSubview:myPDFView];
[self.view addSubview:scrollView];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return myPDFView;
}
EDIT: the zoom work, I use shouldAutorotateToInterfaceOrientation that was the problem (I need to rewrite some code), now it work BUT when I zoom quality worsens. :(

UIScrollView offsetting content double the requested amount

The Issue
My PSWSnapshotView's end up exactly snapshotIndex too far to the right inside the _scrollView (40px offset). Yet, they are set with their frame to have the first one at (0, 0), though that first one shows up at (snapshotInset, 0) instead :/.
The Code
- (void)layoutViews
{
CGRect frame = self.frame;
CGFloat pageWidth = frame.size.width - (snapshotInset * 2);
CGRect pageFrame = CGRectMake(0.0f, 0.0f, pageWidth, frame.size.height);
NSInteger pageCount = [applications count];
[pageControl setNumberOfPages:pageCount];
[scrollView setFrame:CGRectMake(snapshotInset, 0.0f, pageWidth, frame.size.height)];
[scrollView setContentSize:CGSizeMake((pageWidth * pageCount) + 1.0f, frame.size.height)];
for (int i = 0; i < pageCount; i++) {
PSWSnapshotView *view;
if ([snapshotViews count] <= i)
{
PSWSnapshotView *view = [[PSWSnapshotView alloc] initWithFrame:pageFrame application:[applications objectAtIndex:i]];
view.delegate = self;
[scrollView addSubview:view];
[snapshotViews addObject:view];
[view release];
}
else
{
view = [snapshotViews objectAtIndex:i];
[view setFrame:pageFrame];
}
pageFrame.origin.x += pageWidth;
}
}
The Values
self.frame.size.width = 320;
self.frame.size.height = 370;
snapshotInset = 40;
[applications count] = 2;
All the sizes and frames are set correctly (I checked w/ NSLog).
Are you possibly setting the scrollView.contentOffset somewhere, independent of this? Also, and this may just be a copy/paste issue, you reference both snapshotInset and _snapshotInset.