Is there a way to get the filesize in KB from a UIImage, without getting that image from didFinishPickingMediaWithInfo? The images that are presented are coming from the photo album.
I tried the following code, but this gives the following result: size of image in KB: 0.000000
- (void)setImage:(UIImage *)image
{
_image = image;
self.imageView.image = image;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setupView];
self.backgroundColor = [UIColor whiteColor];
panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(beingDragged:)];
[self addGestureRecognizer:panGestureRecognizer];
// prepare image view
self.imageView = [[UIImageView alloc]initWithFrame:self.bounds];
self.imageView.clipsToBounds = YES;
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
[self addSubview:self.imageView];
NSData *imgData = [[NSData alloc] initWithData:UIImageJPEGRepresentation((_image), 0.5)];
int imageSize = imgData.length;
NSLog(#"size of image in KB: %f ", imageSize/1024.0);
overlayView = [[OverlayView alloc]initWithFrame:CGRectMake(self.frame.size.width/2-100, 0, 100, 100)];
overlayView.alpha = 0;
[self addSubview:overlayView];
}
return self;
}
Here is an example of calculating file sizes of files in your HomeDirectory or Documents:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"yourimagename.png"]
File sie is calculated:
filesize = [[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil] fileSize];
NSLog(#"%lld",filesize);
Before you do that add filesize, you can add it in the .m file
#interface ViewController () {
long long filesize;
}
this will result in bytes, if you are trying to convert those bytes into kb you can use the NSByteCountFormatter and it will take care of all the math for you:
NSByteCountFormatter *sizeFormatter = [[NSByteCountFormatter alloc] init];
sizeFormatter.countStyle = NSByteCountFormatterCountStyleFile;
and then call it like so :
[sizeFormatter stringFromByteCount:filesize]
If the image is not saved on the disk you can calculate the size this way:
NSData *imgData = UIImageJPEGRepresentation(_image, 1);
filesize = [imgData length]; //filesize in this case will be an int not long long so use %d to NSLog it
initWithFrame: runs before setImage: is called, so _image is nil at the time you are doing your calculations. You could move them into the setImage: function...
However, this is a weird way of measuring the size of the image. A JPEG file and what ends up in graphics memory are widely different, so if you are doing it for profiling reasons, this is not going to give you any accurate measurements. If you just want the size on disk, you can simply check that through NSFileManager.
NSInteger imageDataSize = CGImageGetHeight(image.CGImage) * CGImageGetBytesPerRow(image.CGImage);
Related
1) In my application, I want to zoom and pinch an image. In xib file, there is a imageView inside a scrollview.Page control is used for swiping images. For a single image it gets zoomed, but for multiple images(coming from web service) it is not getting swipe and zoom.
In setMultipleImages method, images are not getting zoomed and swiped, while if image is single it gets zoom properly.
2) In UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]]; line code, want to replace uiimage with uiimageview. How can I do it?
My code is:
-(void)setMultipleImages{
for (int i = 0; i < [imageMArray count]; i++){
NSString *logoName;
NSArray *imageNameArray = [[imageMArray objectAtIndex:i] componentsSeparatedByString:#"/"];
logoName = [[NSString alloc]initWithFormat:#"%#",[imageNameArray lastObject]];
if (![[AppDelegateHelper getInstance] fileExist:logoName])
{
NSString *imageURL = [[NSString alloc]initWithFormat:#"%#%#",[UrlBuilder getWebsiteUrl],[imageMArray objectAtIndex:i]];
[self startProcessing];
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]];
[activityIndicator stopAnimating];
DebugLog(#"%f,%f",image.size.width,image.size.height);
NSArray *docDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [docDir objectAtIndex:0];
DebugLog(#"saving png");
NSString *pngFilePath = [NSString stringWithFormat:#"%#/%#",cacheDirectory,[imageNameArray lastObject]];
NSData *data1 = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];
[data1 writeToFile:pngFilePath atomically:YES];
}
}
[self loadImageData];
}
-(void)loadImageData
{
int X =0;
for (int s=0; s<imageMArray.count; s++) {
NSString *logoName;
NSArray *imageNameArray = [[imageMArray objectAtIndex:s] componentsSeparatedByString:#"/"];
logoName = [[NSString alloc]initWithFormat:#"%#",[imageNameArray lastObject]];
if([[AppDelegateHelper getInstance] loadImage:logoName] != nil)
{
// UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(X, 0, scrollView.frame.size.width, 300)] ;
[imageView setImage:[[AppDelegateHelper getInstance] loadImage:logoName]];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[scrollView addSubview: imageView];
X = X + imageView.frame.size.width;
}
else
{
//UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(X, 0, scrollView.frame.size.width, 300)] ;
[imageView setImage:[UIImage imageNamed:#"NoImage.png"]];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[scrollView addSubview: imageView];
X = X + imageView.frame.size.width;
}
}
self.pageControl.numberOfPages = [imageMArray count];
scrollView.contentSize = CGSizeMake(X,scrollView.frame.size.height);
lblCount.text = [[NSString alloc]initWithFormat:#"1 sur %lu",(unsigned long)imageMArray.count];
}
Add all your images to imageview. Then apply pinchguesture to images view for zoom effect.
Pinch Guesture:
UIPinchGestureRecognizer *imageviewguesture=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:#selector(ImageViewPinchGuesture:)];
add pinch guesture to your image view:
[YourImageView addGestureRecognizer:imageviewguesture];
Make your selector method:
-(void)ImageViewPinchGuesture:(UIPinchGestureRecognizer *)pinGuestureregonizer{
pinGuestureregonizer.view.transform = CGAffineTransformScale(pinGuestureregonizer.view.transform, pinGuestureregonizer.scale, pinGuestureregonizer.scale);
pinGuestureregonizer.scale = 1;
}
In your .h file add this line
-(void)ImageViewPinchGuesture:(UIPinchGestureRecognizer *)pinGuestureregonizer;
To check effect in simulator Press Cmd+alt then press left mouse pad button and drag.
Hope this will Help. Don't forget to accept the answer if it helps you.
In my project is using maximum 60 images and One of my feature is needs to be an automatically crop all the 60 images in a given Ratio. I'm using the for loop for this implementation.
Inside the for loop contains crop and save the images. It was Implemented. But My app Meets crash in device because of Due to Memory pressure. Please Help Me
for (int ref=0; ref<[_selectedPhotosCollectionthumb count];ref++)
{
UIScrollView *scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0,biManager.screenSize.height/2,biManager.screenSize.width,biManager.screenSize.height/2)];
[scrollView setDelegate:self];
[scrollView setBackgroundColor:[UIColor clearColor]];
[self addSubview:scrollView];
// scrollView.backgroundColor=[UIColor blueColor];
scrollView.userInteractionEnabled=YES;
scrollView.scrollEnabled=YES;
scrollView.tag=ref;
scrollView.hidden=YES;
[_scrollViews addObject:scrollView];
NSLog(#"%i",[_selectedPhotosCollection count]);
NSMutableArray *arrayCell=[_productCollectionsDict valueForKey:[_selectedPhotosCollection objectAtIndex:ref]];
int heightV=0;
for (int cellIndex=0;cellIndex<[arrayCell count];cellIndex++)
{
PrintCellView *cellObj=[arrayCell objectAtIndex:cellIndex];
if(cellObj.pCount>0)
{
PrintEditCellView *cell;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
cell=[[PrintEditCellView alloc] initWithFrame:CGRectMake(0,heightV*100,biManager.screenSize.width,100)];
scrollView.contentSize=CGSizeMake(0,heightV*100+100);
cell.delegate=self;
}
else
{
cell=[[PrintEditCellView alloc] initWithFrame:CGRectMake(0,heightV*50,biManager.screenSize.width,50)];
scrollView.contentSize=CGSizeMake(0,heightV*50+50);
cell.delegate=self;
}
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"hh:mm:ss";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSLog(#"The Current Time is %#",[dateFormatter stringFromDate:now]);
// NSData *imageData=UIImageJPEGRepresentation(Thumbimage,1.0);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:[_selectedPhotosCollection objectAtIndex:ref]];
NSData *data = [[NSMutableData alloc] initWithContentsOfFile:path];
UIImage *image1=[[UIImage alloc]initWithData:data];
cell.productName.text=cellObj.productName.text;
UIImage * image=[self imageByCropping:image1 CropRatio:cell.productName.text];
NSLog(#"CROPPPP");
NSData *imageData= [[NSData alloc] initWithData:UIImageJPEGRepresentation(image,1.0)];
//
NSString* path1 = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"Prydex%i%#.jpg",cellIndex,[dateFormatter stringFromDate:now]]];
NSLog(#"pthhh:%#",path1);
[imageData writeToFile:path1 atomically:YES];
cell.editedImageURL=path1;
NSLog(#"%#,%i",cellObj.productName.text,cellObj.pCount);
[scrollView addSubview:cell];
[cell release];
heightV=heightV+1;
[dateFormatter release];
[image1 release];
// [imageData release];
// [image release];
}
}
//NSLog(#"Scroll Count %i",[_scrollViews count]);
for (UIScrollView *scrollView in _scrollViews)
{
if (scrollView.tag==0)
{
scrollView.hidden=NO;
}
else
{
scrollView.hidden=YES;
}
}
[SVProgressHUD dismiss];
}
Cropping Code
- (UIImage *)imageByCropping:(UIImage *)image CropRatio:(NSString*)ratio
{
CGSize size;
NSArray *array=[ratio componentsSeparatedByString:#"*"];
NSString *productWidth=[array objectAtIndex:0];
NSString *productHeight=[array objectAtIndex:1];
NSLog(#"SIZE:%#,%#",productWidth,productHeight);
NSLog(#"SIZE:%f,%f",image.size.width,image.size.height);
if (image.size.width/[productWidth intValue]>=230)
{
if (image.size.height/[productHeight intValue]>=230) {
size=CGSizeMake([productWidth intValue]*230,[productHeight intValue]*230);
NSLog(#"SIZE Inner:%i,%i",[productWidth intValue],[productHeight intValue]);
}
else if(image.size.width/[productWidth intValue]>=100)
{
if (image.size.height/[productHeight intValue]>=100)
{
size=CGSizeMake([productWidth intValue]*100,[productHeight intValue]*100);
NSLog(#"SIZE outer:%i,%i",[productWidth intValue],[productHeight intValue] );
}
}
}
else if(image.size.width/[productWidth intValue]>=100)
{
if (image.size.height/[productHeight intValue]>=100)
{
size=CGSizeMake([productWidth intValue]*100,[productHeight intValue]*100);
NSLog(#"SIZE outer:%i,%i",[productWidth intValue],[productHeight intValue] );
}
}
NSLog(#"crop---->%#",NSStringFromCGSize(size));
double x = (image.size.width - size.width) / 2.0;
double y = (image.size.height - size.height) / 2.0;
CGRect cropRect = CGRectMake(x, y, size.width, size.height);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return cropped;
}
The solution might be using recursion:
Create a method that takes an array of images you need to process. Inside the method check if array count is zero.
If it is empty you should return, possibly doing some callback to notify the application your image processing is done.
If the array is not empty, take the first image from the array, do all the processing, then remove the first object from the array and call the same method with the new array missing that element. The call should be kind of
[self performSelector:#selector(methodName:) withObject:imageArray];
All together should look something like this:
- (void)processImages:(NSArray *)images {
if(images.count < 1) {
[self performSelectorOnMainThread:#selector(imageProcessingDone) withObject:nil waitUntilDone:NO];
}
else {
UIImage *toProcess = images[0];
NSMutableArray *newArray = [images mutableCopy];
[newArray removeObjectAtIndex:0];
//do the processing
[self performSelector:#selector(processImages:) withObject:newArray];
}
}
I am opening the camera with UIImagePickerControllerSourceTypeCamera and a custom cameraOverlayView so I can take multiple photos without the "Use Photo" step.
This is working great, but there is a memory leak in the save photo function. Through a lot of debugging and research I have narrowed it down to the UIGraphicsGetImageFromCurrentImageContext function.
Here is a snippet of code where it happens:
UIGraphicsBeginImageContextWithOptions(timestampedImage.frame.size, timestampedImage.opaque, [[UIScreen mainScreen] scale]);
[[timestampedImage layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *finalTimestampImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
I have scoured the internet and it seems that the UIGraphicsGetImageFromCurrentImageContext() function (quoted from this SO question) "returns a new autoreleased UIImage and points the finalTimestampImage ivar to it. The previously allocated UIImage is never actually released, the variable to it is just repointed to somewhere else."
I've tried so many solutions that have apparently worked for others:
Adding timestampedImage.layer.contents = nil; after UIGraphicsEndImageContext
Adding CGContextRef context = UIGraphicsGetCurrentContext(); and CGContextRelease(context); after UIGraphicsEndImageContext
Wrapping the above snippet in an NSAutoreleasePool
Wrapping the entire saveThisPhoto function in an NSAutoreleasePool
Creating an NSAutoreleasePool when the camera pops up and calling the [pool release] when didReceiveMemoryWarning is called
Closing the camera popup when didReceiveMemoryWarning is called, hoping it will clear the pool
Every possibly combination of the above
Yet everything I try, when I take photos I can see the Memory Utilized rising and not falling when I'm repeatedly taking photos on the device.
Does anyone know how I can release the autorelease object created by UIGraphicsGetImageFromCurrentImageContext?
Alternatively, is there an alternative way to make a UIImage out of an UIImageView?
Edit:
Here are the full functions as requested. There's a lot of extra releasing added in there just to try make sure everything is cleaned up. I have gone through and tested for the memory leak with each code block in saveThisPhoto systematically, and it only occurs when the UIGraphicsGetImageFromCurrentImageContext block (snippet above) is run.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(#"SAVING PHOTO");
[self saveThisPhoto:info];
picker = nil;
[picker release];
info = nil;
[info release];
}
- (void)saveThisPhoto:(NSDictionary *)info {
// Get photo count for filename so we're not overriding photos
int photoCount = 0;
if ([[NSUserDefaults standardUserDefaults] objectForKey:#"photocount"]) {
photoCount= [[[NSUserDefaults standardUserDefaults] objectForKey:#"photocount"] intValue];
photoCount++;
}
[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:#"%d", photoCount] forKey:#"photocount"];
[[NSUserDefaults standardUserDefaults] synchronize];
// Obtaining saving path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:#"ri_%d.jpg", photoCount];
NSString *fileNameThumb = [NSString stringWithFormat:#"ri_%d_thumb.jpg", photoCount];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSString *imagePathThumb = [documentsDirectory stringByAppendingPathComponent:fileNameThumb];
// Extracting image from the picker and saving it
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
// SAVE TO IPAD AND DB
if ([mediaType isEqualToString:#"public.image"]){
// Get Image
UIImage *editedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
// Figure out image orientation
CGSize resizedSize;
CGSize thumbSize;
if (editedImage.size.height > editedImage.size.width) {
resizedSize = CGSizeMake(480, 640);
thumbSize = CGSizeMake(150, 200);
} else {
resizedSize = CGSizeMake(640, 480);
thumbSize = CGSizeMake(150, 113);
}
// MAKE NORMAL SIZE IMAGE
UIImage *editedImageResized = [editedImage resizedImage:resizedSize interpolationQuality:0.8];
// clean up the one we won't use any more
editedImage = nil;
[editedImage release];
// ADD TIMESTAMP TO IMAGE
// make the view
UIImageView *timestampedImage = [[UIImageView alloc] initWithImage:editedImageResized];
CGRect thisRect = CGRectMake(editedImageResized.size.width - 510, editedImageResized.size.height - 30, 500, 20);
// clean up
editedImageResized = nil;
[editedImageResized release];
// make the label
UILabel *timeLabel = [[UILabel alloc] initWithFrame:thisRect];
timeLabel.textAlignment = UITextAlignmentRight;
timeLabel.textColor = [UIColor yellowColor];
timeLabel.backgroundColor = [UIColor clearColor];
timeLabel.font = [UIFont fontWithName:#"Arial Rounded MT Bold" size:(25.0)];
timeLabel.text = [self getTodaysDateDatabaseFormat];
[timestampedImage addSubview:timeLabel];
// clean up what we won't use any more
timeLabel = nil;
[timeLabel release];
// make UIIMage out of the imageview -- MEMORY LEAK LOOKS LIKE IT IS IN THIS BLOCK
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIGraphicsBeginImageContextWithOptions(timestampedImage.frame.size, timestampedImage.opaque, [[UIScreen mainScreen] scale]);
[[timestampedImage layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *finalTimestampImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
timestampedImage.layer.contents = nil;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextRelease(context);
// clean up the one we won't use any more
timestampedImage = nil;
[timestampedImage release];
// SAVE NORMAL SIZE IMAGE TO DOCUMENTS FOLDER
NSData *webDataResized = UIImageJPEGRepresentation(finalTimestampImage, 1.0); // JPG
[webDataResized writeToFile:imagePath atomically:YES];
// clean up the one we won't use any more
finalTimestampImage = nil;
[finalTimestampImage release];
[pool release]; // to get rid of the context image that is stored
// SAVE TO DATABASE
[sqlite executeNonQuery:#"INSERT INTO inspection_images (agentid,groupid,inspectionid,areaid,filename,filenamethumb,filepath,orderid,type) VALUES (?, ?, ?, ?, ?, ?, ?, ?,?) ",
[NSNumber numberWithInt:loggedIn],
[NSNumber numberWithInt:loggedInGroup],
myInspectionID,
[[tableData objectAtIndex:alertDoMe] objectForKey:#"areaid"],
fileName,
fileNameThumb,
documentsDirectory,
[NSNumber numberWithInt:photoCount],
[NSNumber numberWithInt:isPCR]
];
// Clean up
webDataResized = nil;
[webDataResized release];
} else {
NSLog(#">>> IMAGE ***NOT*** SAVED");
}
NSLog(#"IMAGE SAVED - COMPLETE");
info = nil;
[info release];
}
You're setting your variables to nil before releasing them, and some are already auto released.
Normally when using release you should release and them set to nil.
[var release]
var = nil;
But in some of these you should not be calling release.
The following one is your main culprit.
// clean up the one we won't use any more
timestampedImage = nil;
[timestampedImage release];
// SAVE NORMAL SIZE IMAGE TO DOCUMENTS FOLDER
I am working on an app in which I need to load nearly 211 images in a scrollview.
What I was doing was I was converting the images in binary format (NSData) and saving them in core data. Then retrieving them and populating them on the scrollview.
it does work, but sometimes it does throw "Memory warning".
I learned that lazy loading is a way to achieve this. But, I am not totally aware of how it is done.
Like, loading 3 or 5 images ahead/back of the image visible currently on the scrollview.
Can any bode help me with a simple example, which would help me understand from scratch ? and would point me in the right direction.
Thank you for your time.
Well, it is not needed to store images into core data. Just take all image URLs into NSMutableArray and try to use following code as per your requirement.Hope this helps you.
Try this code -
friendsScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 470, 320, 100)];
friendsScrollView.contentSize = CGSizeMake([meetUPFrndNameArray count] * 95,50);
friendsScrollView.backgroundColor = [UIColor clearColor];
friendsScrollView.delegate = self;
[self.view addSubview:friendsScrollView];
int imageX = 25,imageY = 20;
int backImageX = 10, backImageY = 10;
int flag = 0;
for (int i = 0; i < [meetUPFrndPhotoArray count]; i++) {
flag ++;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = paths[0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:meetUPFrndPhotoArray[flag-1]];
UIImage *img1 = [UIImage imageWithContentsOfFile:savedImagePath];
if (!img1 || [UIImagePNGRepresentation(img1) length] <=0)
{
id path = meetUPFrndPhotoArray[flag-1];
path = [path stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSURL *url = [NSURL URLWithString:path];
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:url, [NSString stringWithFormat:#"%d", flag], nil];
[self performSelectorInBackground:#selector(loadImageInBackground:) withObject:arr];
UIImageView *frndBackgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(backImageX, backImageY, 80, 80)];
frndBackgroundImage.image = [UIImage imageNamed:#"friend_image_background.png"];
frndBackgroundImage.backgroundColor = [UIColor clearColor];
frndBackgroundImage.layer.borderWidth = 2.0;
frndBackgroundImage.layer.masksToBounds = YES;
frndBackgroundImage.layer.shadowOffset = CGSizeMake(0, 1);
frndBackgroundImage.layer.shadowOpacity = 1;
frndBackgroundImage.layer.shadowRadius = 1.2;
frndBackgroundImage.layer.cornerRadius = 5.0;
frndBackgroundImage.layer.borderColor = [[UIColor clearColor] CGColor];
[friendsScrollView addSubview:frndBackgroundImage];
UIImageView *friendImage = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, 50, 50)];
friendImage.tag = flag;
friendImage.image = [UIImage imageNamed:#"ic_user.png"];
[friendsScrollView addSubview:friendImage];
}
else
{
UIImageView *frndBackgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(backImageX, backImageY, 80, 80)];
frndBackgroundImage.image = [UIImage imageNamed:#"friend_image_background.png"];
frndBackgroundImage.backgroundColor = [UIColor clearColor];
frndBackgroundImage.layer.borderWidth = 2.0;
frndBackgroundImage.layer.masksToBounds = YES;
frndBackgroundImage.layer.shadowOffset = CGSizeMake(0, 1);
frndBackgroundImage.layer.shadowOpacity = 1;
frndBackgroundImage.layer.shadowRadius = 1.2;
frndBackgroundImage.layer.cornerRadius = 5.0;
frndBackgroundImage.layer.borderColor = [[UIColor clearColor] CGColor];
[friendsScrollView addSubview:frndBackgroundImage];
UIImageView *friendImage = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, 50, 50)];
friendImage.tag = flag-1;
friendImage.image = img1;
[friendsScrollView addSubview:friendImage];
}
backImageX = backImageX + 80 + 10;
backImageY = 10;
imageX = imageX + 50 + 25 + 15;
imageY = 20;
}
Here meetUPFrndPhotoArray contains image URLs.
And
For Downloading images in Background -
- (void) loadImageInBackground:(NSArray *)urlAndTagReference{
NSData *imgData = [NSData dataWithContentsOfURL:urlAndTagReference[0]];
UIImage *img = [[UIImage alloc] initWithData:imgData];
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:img, urlAndTagReference[1], nil];
[self performSelectorOnMainThread:#selector(assignImageToImageView:) withObject:arr waitUntilDone:YES];
}
For Assigning downloaded image to perticular imageView -
- (void) assignImageToImageView:(NSArray *)imgAndTagReference{
for (UIImageView *checkView in [friendsScrollView subviews] ){
if ([imgAndTagReference count] != 0) {
if ([checkView tag] == [imgAndTagReference[1] intValue]){
[checkView setImage:imgAndTagReference[0]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = paths[0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:meetUPFrndPhotoArray[[imgAndTagReference[1] intValue]-1]];
UIImage* imageToSave = [checkView image];
NSData *imageData = UIImagePNGRepresentation(imageToSave);
[imageData writeToFile:savedImagePath atomically:NO];
}
}
}
}
Let me know if this helps you.Thanks in advance.All the best.
Make use of delegate functions, content size and content inset properties of the scrollview
Checkout this library It's a generic UIScrollView that reuses it's subviews like UITableView does (there is a dataSource object which is used to configure scroll view subviews)
Hi I have some code that mostly works. It basically animates, with a fade affect, number of images from an array.
Basically it all works well however I have a slight bug that when the view first loads the first image only in the array seems to rotate 90 degrees to the correct position and then fades to the same image but its annoying to see animate like this. All the fading from image to another is perfect just with the above mentioned bug. And as mentioned is just on first load.
Here is the code. images is an MutableArray and both topImageView and bottomImageView both are ivars and ** synthesized**. So is the images array, too.
int topIndex = 0, bottomIndex = 1;
-(void)imageFader{
self.imageViewBottom = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 370)];
[self.view addSubview:imageViewBottom];
[imageViewBottom setAlpha:0.0];
[imageViewBottom release];
self.imageViewTop = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 370)];
[self.view addSubview:imageViewTop];
[imageViewTop setAlpha:1.0];
[imageViewTop release];
[imageViewTop setImage:[images objectAtIndex:topIndex]];
timer = [NSTimer timerWithTimeInterval:1.0
target:self
selector:#selector(onTimer)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
[timer fire];
}
-(void)onTimer{
[UIView animateWithDuration:1 animations:^{
//set the image of the image that is not currently visible
if (imageViewTop.alpha == 0) {
[imageViewTop setImage:[images objectAtIndex:topIndex]];
}
if (imageViewBottom.alpha == 0) {
[imageViewBottom setImage:[images objectAtIndex:bottomIndex]];
}
//make sure the images rotate
[imageViewTop setAlpha:imageViewTop.alpha == 0 ? 1 : 0];
[imageViewBottom setAlpha:imageViewBottom.alpha == 0 ? 1 : 0];
//make sure the images play in a loop
topIndex = topIndex < [images count]-1 ? bottomIndex+1 : 0;
bottomIndex = bottomIndex < [images count]-1 ? bottomIndex+1 : 0;}];
}
And here is how the images are saved to the docs directory before being loaded in to the MutableArray for the animation.
-(void) imagePickerController:(UIImagePickerController *) picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
NSMutableDictionary *metadata = [[NSMutableDictionary alloc] init];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
//Save to camera roll
[library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation) ALAssetOrientationRight completionBlock:^(NSURL *assetURL, NSError *error) { NSLog(#"completionBlock");
}];
int imageNumber = 0;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *pathToFile;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
do
{
// increment the image
imageNumber++;
// get the new path to the file
pathToFile = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:
#"standingImage%d.JPG", imageNumber]];
}
while([fileManager fileExistsAtPath:pathToFile]);
/* so, we loop for as long as we keep coming up with names that already exist */
UIImage *image2 = image; // imageView is my image from camera
//Use UIImageJPEGRepresentation to maitain image orientation!
NSData *imageData = UIImageJPEGRepresentation(image2, 0.9);
[imageData writeToFile:pathToFile atomically:NO];
[self dismissModalViewControllerAnimated:YES];
This unexpected orientation may be caused by the origin of the images - the UIImagePickerController. Try accessing the JPEG files you mentioned in your comment and check their orientation, then you'll at least narrow down the potential cause of your issue.