i have try to create an Image Controller for store image and reuse it when need during one instance of application, this is my class
[Nota: the method controllaDictionary search the key of the image if is was just used (explain on detail above)]
-(void)imageContentUrl:(NSString*)imageURL andImage:(UIImage**)image{
NSLog(#"Stampa Immagine Contente URL: %#",imageURL);
NSString* imageName = [[imageURL componentsSeparatedByString:#"/"] lastObject];
if([self controllaDictionary:imageName]){
NSLog(#"||| IMMAGES ---------> Image Exixt in Dictionary");
UIImage* imageInDict = [self controllaDictionary:imageName];
image = &imageInDict;
return;
}else{
UIImage* imageFile = nil;
if([imageName isEqualToString:#""] || imageName == nil){
NSLog(#"||| IMMAGES ------- Null Link");
image = nil;
}
if([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:#"%#/%#",_imageDirectory,imageName]]){
NSLog(#"||| IMMAGES ------- Exist in Folder: %#",[NSString stringWithFormat:#"%#/%#",_imageDirectory,imageName]);
imageFile = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:#"%#/%#",_imageDirectory,imageName]];
image = &imageFile;
[self.imageDict setValue:imageFile forKey:imageName];
return;
}else{
NSLog(#"||| IMMAGINI ------- Miss, Start Download at URL: %#",imageURL);
[self downloadImageAtUrl:imageURL andImagePointer:image withHendlerBlock:^(NSError *error, UIImage *image, NSString *urlImage,UIImage** doubleImagePointer) {
if (!error) {
NSLog(#"Stampa Double Pointer IMG -> %#",image);
doubleImagePointer = ℑ
[self saveImage:image andName:imageName];
[self.imageDict setValue:image forKey:imageName];
}else{
NSLog(#"Stampa Errore: %#",error);
}
}];
}
}
}
-(void)downloadImageAtUrl:(NSString*)urlImage andImagePointer:(UIImage**)image withHendlerBlock:(void(^)(NSError* error,UIImage* imageDownloaded,NSString* urlImage,UIImage** doubleImagePointer))dowloadImageBlock{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
UIImage* downloadImg = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlImage]]];
NSError* error = nil;
if (downloadImg) {
NSLog(#"Immagine Acquisita");
}else{
NSLog(#"Errore Download Compilazione Errore");
error = [NSError errorWithDomain:DownloadErrorDomain code:0 userInfo:#{DownloadErrorDomain: #"Errore nel download dell'immagine, non si hanno a disposizione ulteriori strumenti per comprenderne la natura. Effettuare un ceck della URL"}];
}
dowloadImageBlock(error,downloadImg,urlImage,image);
});
}
#end
ok after this i explain you how wath i have to do:
QUickly is a singleton instance that search in a specific Folder if there is a Image.
Yes set image and done, No start dowload then set and done.
I have see that this method is powerfull for memory management because i create a NSMutabeDictionary with the name of image like a key and the UIImage for value so i use only one pointer for multiple instance of same image [then i allocate in memory the image only first time].
-(void)imageContentUrl:imageURL andImage:(UIImage**)image
this method search in the dir if file exist and set the image-> now i would operate like sometime run NSError so i pass to this method an UIImage**
like this
UIImage* image = nil;
[[GestoreImmagini instance] imageContentUrl:#"URL/Immage/For/Download.jpg" andImage:&image];
[self.imageEvent setImage:image];
the in async i set the image.
this is my Idea but the image are downloaded and set (and if i log, are printed on console) but self.imageEvent don't show the image... Some one can help me?
#Edit After Cy-4AH Answer
Correct!! Is my fault... after C++ i have pass more time to work without pointer and i have forgot somting...
tnx but now the second portion of the code after dowload and the return from the block the image was not set... [with correction of *]
[self downloadImageAtUrl:imageURL andImagePointer:image withHendlerBlock:^(NSError *error, UIImage *image, NSString *urlImage,UIImage** doubleImagePointer) {
if (!error) {
NSLog(#"Stampa Double Pointer IMG -> %#",image);
*doubleImagePointer = image;
[self saveImage:image andName:imageName];
[self.imageDict setValue:image forKey:imageName];
}else{
NSLog(#"Stampa Errore: %#",error);
}
}];
why? on theory the pointer is the same...
#Edit After DEBUG
i have found problem here at the log 2.0 , 2.1 and 2.2 the image print (null) and this i correct because i not have set it but in 2.3 i have a crash for BAD_ACCESS like miss all References. and same happens when i print it in the dowloadImageBlock().
-(void)downloadImageAtUrl:(NSString*)urlImage andImagePointer:(UIImage**)image withHendlerBlock:(void(^)(NSError* error,UIImage* imageDownloaded,NSString* urlImage,UIImage** doubleImagePointer))dowloadImageBlock{
NSLog(#" 2.0 - ||| ----- -------- -----------> %#",*image);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
NSLog(#" 2.1 - ||| ----- -------- -----------> %#",*image);
dispatch_async(queue, ^{
NSLog(#" 2.2 - ||| ----- -------- -----------> %#",*image);
UIImage* downloadImg = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlImage]]];
NSError* error = nil;
if (downloadImg) {
NSLog(#"Immagine Acquisita");
}else{
NSLog(#"Errore Download Compilazione Errore");
error = [NSError errorWithDomain:DownloadErrorDomain code:0 userInfo:#{DownloadErrorDomain: #"Errore nel download dell'immagine, non si hanno a disposizione ulteriori strumenti per comprenderne la natura. Effettuare un ceck della URL"}];
}
NSLog(#" 2.3 - ||| ----- -------- -----------> %#",*image);
dowloadImageBlock(error,downloadImg,urlImage,image);
});
}
You have missanderstanding in working with pointers.
It's should be:
*image = imageInDict;
Also you download image asynchroniously, and yours variable located in function call stack is already destroyed, when you try assign value to it. You should better use UIImageView* or some other objective-c class that have UIImage* in properties instead UIImage**
Related
I'm trying to get all photos from photos library with image's metadata. It works fine for 10-20 images but when there are 50+ images it occupies too much memory, which causes to app crash.
Why i need all images into array?
Answer - to send images to server app. [i'm using GCDAsyncSocket to send data on receiver socket/port and i don't have that much waiting time to request images from PHAsset while sending images on socket/port.
My Code :
+(void)getPhotosDataFromCamera:(void(^)(NSMutableArray *arrImageData))completionHandler
{
[PhotosManager checkPhotosPermission:^(bool granted)
{
if (granted)
{
NSMutableArray *arrImageData = [NSMutableArray new];
NSArray *arrImages=[[NSArray alloc] init];
PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];
NSLog(#"%d",(int)result.count);
arrImages = [result copy];
//--- If no images.
if (arrImages.count <= 0)
{
completionHandler(nil);
return ;
}
__block int index = 1;
__block BOOL isDone = false;
for (PHAsset *asset in arrImages)
{
[PhotosManager requestMetadata:asset withCompletionBlock:^(UIImage *image, NSDictionary *metadata)
{
#autoreleasepool
{
NSData *imageData = metadata?[PhotosManager addExif:image metaData:metadata]:UIImageJPEGRepresentation(image, 1.0f);
if (imageData != nil)
{
[arrImageData addObject:imageData];
NSLog(#"Adding images :%i",index);
//--- Done adding all images.
if (index == arrImages.count)
{
isDone = true;
NSLog(#"Done adding all images with info!!");
completionHandler(arrImageData);
}
index++;
}
}
}];
}
}
else
{
completionHandler(nil);
}
}];
}
typedef void (^PHAssetMetadataBlock)(UIImage *image,NSDictionary *metadata);
+(void)requestMetadata:(PHAsset *)asset withCompletionBlock:(PHAssetMetadataBlock)completionBlock
{
PHContentEditingInputRequestOptions *editOptions = [[PHContentEditingInputRequestOptions alloc]init];
editOptions.networkAccessAllowed = YES;
[asset requestContentEditingInputWithOptions:editOptions completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info)
{
CIImage *CGimage = [CIImage imageWithContentsOfURL:contentEditingInput.fullSizeImageURL];
UIImage *image = contentEditingInput.displaySizeImage;
dispatch_async(dispatch_get_main_queue(), ^{
completionBlock(image,CGimage.properties);
});
CGimage = nil;
image = nil;
}];
editOptions = nil;
asset =nil;
}
+ (NSData *)addExif:(UIImage*)toImage metaData:(NSDictionary *)container
{
NSData *imageData = UIImageJPEGRepresentation(toImage, 1.0f);
// create an imagesourceref
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef) imageData, NULL);
// this is the type of image (e.g., public.jpeg)
CFStringRef UTI = CGImageSourceGetType(source);
// create a new data object and write the new image into it
NSMutableData *dest_data = [[NSMutableData alloc] initWithLength:imageData.length+2000];
CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)dest_data, UTI, 1, NULL);
if (!destination) {
NSLog(#"Error: Could not create image destination");
}
// add the image contained in the image source to the destination, overidding the old metadata with our modified metadata
CGImageDestinationAddImageFromSource(destination, source, 0, (__bridge CFDictionaryRef) container);
BOOL success = NO;
success = CGImageDestinationFinalize(destination);
if (!success) {
NSLog(#"Error: Could not create data from image destination");
}
CFRelease(destination);
CFRelease(source);
imageData = nil;
source = nil;
destination = nil;
return dest_data;
}
Well it's not a surprise that you arrive into this situation, since each of your image consumes memory and you instantiate and keep them in memory. This is not really a correct design approach.
In the end it depends on what you want to do with those images.
What I would suggest is that you keep just the array of your PHAsset objects and request the image only on demand.
Like if you want to represent those images into a tableView/collectionView, perform the call to
[PhotosManager requestMetadata:asset withCompletionBlock:^(UIImage *image, NSDictionary *metadata)
directly in the particular method. This way you won't drain the device memory.
There simply is not enough memory on the phone to load all of the images into the photo library into memory at the same time.
If you want to display the images, then only fetch the images that you need for immediate display. For the rest keep just he PHAsset. Make sure to discard the images when you don't need them any more.
If you need thumbnails, then fetch only the thumbnails that you need.
If want to do something with all of the images - like add a watermark to them or process them in some way - then process each image one at a time in a queue.
I cannot advise further as your question doesn't state why you need all of the images.
I'm creating a photo slideshow app.
My app flow :
user select photo assets > (over 100+)
load images from assets > (display image size)
set imageView image or add CIFilter to image and slides
Question :
When I create CIImage Object from CGImage , the memory is growing up so fast and if the image count over 100+, the app crash.
But it's strange that I remove the create code, the app works fine.
Can anybody help me ?
more code :
- (void)loadDisplayImagesWithCompletion:(void (^)(NSArray *images))completion {
dispatch_async(dispatch_queue_create("PhotosEditViewController_loadImageQueue", nil), ^{
__block NSMutableArray *images = [NSMutableArray array];
__block int handleCount = 0;
__weak PhotosEditViewController *weakSelf = self;
for (PHAsset *asset in self.photoAssets) {
[KPPhotoManager requestImageForAsset:asset targetSize:self.contentView.frame.size completeBlock:^(UIImage *image) {
[images addObject:image];
#autoreleasepool {
CIImage *ciImage = [CIImage imageWithCGImage:[image CGImage]];//memory growing up
}
}];
}
});
}
// KPPhotoManager
+ (void)requestImageForAsset:(PHAsset*)asset targetSize:(CGSize)size completeBlock:(void (^)(UIImage *image))completeBlock
{
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeAspectFill options:[self createImageRequestOptions] resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
completeBlock(result);
}];
}
I created a new project copying your code. I increased the number of calls to 10000 and inspecting the memory all looks well. It does jump a bit but does not inflate.
I also tried to force image load using:
for (int i=0; i<10000; i++) {
#autoreleasepool {
UIImage *image = [UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"image2.png"]];
[self createFilterImageWithImage:image];
}
}
and it still does not inflate the memory.
How are you inspecting your memory leak? Are you looking at the memory consumption? Are there any specifics in your project or file such as having disabled ARC?
I'm using an image picker library to allow the user to select many images from their photo library. They are returned as an array of PHAssets. Then, I want to convert all the PHAssets to UIImages and write them to the app's storage.
At the moment, I'm looping through all the assets and calling requestImageForAsset synchronously. My issue is that there is incredibly high memory usage spike when this loop is being run (with 30 images, it spikes up to 130MB). I would like to prevent this.
Here is my code:
for(PHAsset *asset in self.assets) {
NSLog(#"started requesting image %i", i);
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFit options:[self imageRequestOptions] resultHandler:^(UIImage *image, NSDictionary *info) {
dispatch_async(dispatch_get_main_queue(), ^{
assetCount++;
NSError *error = [info objectForKey:PHImageErrorKey];
if (error) NSLog(#"Image request error: %#",error);
else {
NSString *imagePath = [appDelegate.docsPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%i.png",i]];
NSData *imageData = UIImagePNGRepresentation(image);
if(imageData) {
[imageData writeToFile:imagePath atomically:YES];
[self.imagesArray addObject:imagePath];
}
else {
NSLog(#"Couldn't write image data to file.");
}
[self checkAddComplete];
NSLog(#"finished requesting image %i", i);
}
});
}];
i++;
}
Based on the logs, I see that all of the "starting requesting image x" are called first, then all of the completion blocks ("finished requesting image x"). I think that this might be contributing to the memory issue. It would probably be less memory intensive to ensure that the completion block for each iteration is called before freeing those resources and moving to the next iteration. How can I do this?
#Inder Kumar Rathore trick does not work for me.
So i tried read more about PHImageManager here
I found that if i switch from
- requestImageForAsset:targetSize:contentMode:options:resultHandler:
to
- requestImageDataForAsset:options:resultHandler:
i will receive the image with the same dimension {5376, 2688} but the size in byte is much smaller. So the memory issue is solved.
hope this help !!
(note : [UIImage imageWithData:imageData] use this to convert NSData to UIImage)
Please use autoreleasepool for memory management.
for(PHAsset *asset in self.assets) {
// This autorelease pool seems good (a1)
#autoreleasepool {
NSLog(#"started requesting image %i", i);
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFit options:[self imageRequestOptions] resultHandler:^(UIImage *image, NSDictionary *info) {
dispatch_async(dispatch_get_main_queue(), ^{
//you can add autorelease pool here as well (a2)
#autoreleasepool {
assetCount++;
NSError *error = [info objectForKey:PHImageErrorKey];
if (error) NSLog(#"Image request error: %#",error);
else {
NSString *imagePath = [appDelegate.docsPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%i.png",i]];
NSData *imageData = UIImagePNGRepresentation(image);
if(imageData) {
[imageData writeToFile:imagePath atomically:YES];
[self.imagesArray addObject:imagePath];
}
else {
NSLog(#"Couldn't write image data to file.");
}
[self checkAddComplete];
NSLog(#"finished requesting image %i", i);
}
} //a2 ends here
});
}];
i++;
} // a1 ends here
}
I solved it with a recursive method that ensures that the completion block is finished before next iteration. This way it is possible to get thousands of photos with very low memory.
Here is what it does:
Given an array of indices of selected photos in the camera roll
Request first object in array.
In completion block, remove first object in array and call same method again.
Repeat until array is empty
Here is the code.
- (void)processPhotos
{
NSIndexPath *indexPath = [_selectedPhotosArray objectAtIndex:0];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
PHAsset *asset = [_allPhotos objectAtIndex:indexPath.row];
[self.imageManager requestImageForAsset:asset
targetSize:PHImageManagerMaximumSize
contentMode:PHImageContentModeAspectFill
options:self.imageRequestOptions
resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
NSError *error = [info objectForKey:PHImageErrorKey];
if (_selectedPhotosArray.count > 0) {
[_selectedPhotosArray removeObjectAtIndex:0];
}
if (error) {
NSLog(#"[CameraRoll] Image request error: %#",error);
} else {
if (result != nil) {
[self processImage:result];
}
}
if (_selectedPhotosArray.count > 0) {
// Recurring loop
[self processPhotos];
}
}];
});
}
Make sure to check if array not empty before calling this method for the first time.
The problem is that I am calling multiple webservices on my homepage and the webservice is returning me the images and text from the server. During this process the UI become fully unresponsive for say 1-2 minutes which is looking very bad as I cant do anything. I heard about dispatch and tried to implement it but I dont get any results.May be I am doing something wrong.
What I want now that I want to that I want to run this process in background So that a user can interact with the UI during the fetching operation from the server. I am implementing my code just tell me where to use dispatch.
-(void)WebserviceHomeSlider{
if([AppDelegate appDelegate].isCheckConnection){
//Internet connection not available. Please try again.
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:#"Internate error" message:#"Internet connection not available. Please try again." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alertView show];
return;
}else {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:#"text/html"];
[manager GET:ServiceUrl#"fpMainBanner" parameters:nil success:^(AFHTTPRequestOperation *operation,id responseObject)
{
//NSLog(#"JSON: %#", responseObject);
arrSlider = [responseObject objectWithJSONSafeObjects];
[_slideshow setTransitionType:KASlideShowTransitionSlide];
_slideshow.gestureRecognizers = nil;
[_slideshow addGesture:KASlideShowGestureSwipe];
// [_slideshow addImagesFromResources:[self getImages]]; // Add
// [_slideshow addTextFromResources:[self getText]];
// [slideShow subtextWithImages:[self getsubText]];
[_slideshow addImagesFromResources:[self getImages] stringArray:[self getText] stringsubArray:[self getsubText]];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
}
}
Just tell me where to use dispatch or edit my code using dispatch if possible. I have gone through some examples but still my concept is not clear. Which dispatch method id best (DEFAULT or BACKGROUND). I will be very thankful to You.
This is the code you are looking for . Just tell me where to edit it using dispatch
-(NSArray *)getText{
NSMutableArray *textArr = [[NSMutableArray alloc] init];
for(int i=0; i<[arrSlider count];i++)
{
texxt=[[arrSlider objectAtIndex:i ]valueForKey:#"title" ];
[textArr addObject:[texxt uppercaseString]];
}
return textArr;
}
-(NSArray *)getsubText{
NSMutableArray *subtext = [[NSMutableArray alloc] init];
for(int i=0; i<[arrSlider count];i++)
{
subbtext=[[arrSlider objectAtIndex:i ]valueForKey:#"tagline_value" ];
if(i==8)
{
subbtext=#"MAKE YOURSELF STAND OUT GET YOUR FREE CARDS!";
}
NSLog(#"subtext is,,.,.,,.,%#.%#",#"k",subbtext);
[subtext addObject:[subbtext uppercaseString]];
}
return subtext;
}
-(NSArray *)getImages
{
NSMutableArray *mArr = [[NSMutableArray alloc] init];
for(int i=0; i<[arrSlider count];i++)
{
pathh=[[arrSlider objectAtIndex:i ]valueForKey:#"filepath" ];
NSString *newString = [pathh stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSURL *imageURL = [NSURL URLWithString:newString];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *originalImage = [UIImage imageWithData:imageData];
CGSize destinationSize = CGSizeMake(320, 158);
UIGraphicsBeginImageContext(destinationSize);
[originalImage drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// [whotshotimgview setImage:image];
[mArr addObject: img];
}
return mArr;
}
[_slideshow addImagesFromResources:[self getImages] stringArray:[self getText] stringsubArray:[self getsubText]];
The problem here is that you didn't showed implementation of addImagesFromResources method. You probably have to implement GCD in this method because i guess you are fetching images and setting on UI by this your main thread is getting blocked.
You are trying to access the UIView from thread other than the main which causes the UI unresponsiveness.
Please use this:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[_slideshow setTransitionType:KASlideShowTransitionSlide];
_slideshow.gestureRecognizers = nil;
[_slideshow addGesture:KASlideShowGestureSwipe];
// [_slideshow addImagesFromResources:[self getImages]]; // Add
// [_slideshow addTextFromResources:[self getText]];
// [slideShow subtextWithImages:[self getsubText]];
[_slideshow addImagesFromResources:[self getImages] stringArray:[self getText] stringsubArray:[self getsubText]];
});
What I think is that your UI hangs due to downloading of image.
So you should use SDWebImage library that can help you to cache image and also prevent the UI getting hang. Using SDWebImage you can show the loader for the time the image is not loaded and it will cache the image. So from next time no image will be downloaded and also the UI will not hang. The link for complete reference to SDWebImage is :https://github.com/rs/SDWebImage
This block blocking your main UI to perform tasks so update this block as follow,
for(int i=0; i<[arrSlider count];i++)
{
pathh=[[arrSlider objectAtIndex:i ]valueForKey:#"filepath" ];
NSString *newString = [pathh stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
dispatch_queue_t myQueue = dispatch_queue_create("My Queue",NULL);
dispatch_async(myQueue, ^{
NSURL *imageURL = [NSURL URLWithString:newString];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *originalImage = [UIImage imageWithData:imageData];
CGSize destinationSize = CGSizeMake(320, 158);
UIGraphicsBeginImageContext(destinationSize);
[originalImage drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// [whotshotimgview setImage:image];
[mArr addObject: img];
dispatch_async(dispatch_get_main_queue(), ^{
// If you want to refresh your UI simultaniously, you can write statement for that i.e
// yourImageView.image = img;
// Otherwise remove this queue
});
});
}
The main thread should never be used for long processing. Its there for the UI.
AFNetworking provides asynchronous functionality for downloading files. You should also be careful of thrashing the network layer with too many simulatanous downloads. I think 4 or 5 is the max the last time I tested.
So the flow of execution should be as follows.
The controller sets up the slideshow, sends the downloading of the images to a background thread(automatically handled by AFNetworking), passing a completion block to it. In this completion block you need to dispatch the work back to the main thread using GCD, dispatch_async to dispatch_get_get_main_queue
Keep your main thread's run loop available for user interaction.
It is because the images are loading in another thread and you are populating your slider right after getting the data.
It is better to implement this protocol method of KASlidershow to use the slideshow in a more memory efficient way.
(UIImage *)slideShow:(KASlideShow *)slideShow imageForPosition:(KASlideShowPosition)position
{
pathh=[[arrSlider objectAtIndex:i ]valueForKey:#"filepath" ];
NSString *newString = [pathh stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
dispatch_queue_t myQueue = dispatch_queue_create("My Queue",NULL);
dispatch_async(myQueue, ^{
NSURL *imageURL = [NSURL URLWithString:newString];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *originalImage = [UIImage imageWithData:imageData];
CGSize destinationSize = CGSizeMake(320, 158);
UIGraphicsBeginImageContext(destinationSize);
[originalImage drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// [whotshotimgview setImage:image];
[mArr addObject: img];
return img;
});
}
i didn't try the code but i hope it will work with little changes in variable names. Good luck
I have successfully retrieved the image (lets say user profile pic) from parse, and i'm displaying it in the ios app. But if there is no image in the file column, then i want to display default image which is stored in the locally (not in parse), not successful.
My simple question is, how to check if there is image or not ? Below is the simple code;
PFFile *userImage = [object objectForKey:#"profilePic"]; __block UIImage *userProfilePic =[[UIImage alloc]init];
[userImage getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
//this block will execute only if there is image
if(data) {
userProfilePic = [UIImage imageWithData:data];
userPic.image=userProfilePic;
}
//this block is not at all executing. This block should execute when user doesn't have their profile uploaded
if(!data) {
UIImage *userProfileDefaultPic = [[UIImage alloc]init];
userProfileDefaultPic=[UIImage imageNamed:#"defaultprofile.png"];
userPic.image=userProfileDefaultPic;
}
}
}];
Hope you can help me. Thanks in advance.
Pradeep
At last found the solution !
(!data) will not execute , instead we need to check if the PFFile and the UIImage to store PFFile has image or not, like below;
PFFile *userImage = [object objectForKey:#"profilePic"];
__block UIImage *userProfilePic =[[UIImage alloc]init];
if(userImage && ![userProfilePic isEqual:[NSNull null]]) //This is important to check if the file if image is there or not and if not, then display default pic in else part
{
[userImage getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if(!error)
{
userProfilePic = [UIImage imageWithData:data];
userPic.image=userProfilePic;
}
}];
}
else
{
userProfilePic =[UIImage imageNamed:#"defaultprofilepic.png"];
userPic.image=userProfilePic;
}
Your code already does that. You just need to set your image on the main thread and not background thread.
if(!data) { [dispatch_async(dispatch_get_main_queue(), ^{
UIImage *userProfileDefaultPic = [[UIImage alloc]init];
userProfileDefaultPic=[UIImage imageNamed:#"defaultprofile.png"];
userPic.image=userProfileDefaultPic;
}}];