Ganesh Kumar Palani Ganesh Kumar Palani - 5 months ago 37
iOS Question

How to delete two images of an array from UIImageView in iOS

I have five imageviews which I store the array of images.The issues is, if i delete the first image from an image view and it is not deleting correctly.Here my image index is same which one choosed and after deleting the next image I'm getting error like

[__NSArrayI objectAtIndex:]: index 3 beyond bounds 2


My code snippet here:

-(void)viewWillAppear:(BOOL)animated
{

//[self change_header_colors];

[[NSMutableSet setWithArray:del_imgArray] minusSet:[NSSet setWithArray:assetImages]];


del_imgArray = [[NSMutableArray alloc]init];

del_imgArray = [[[NSUserDefaults standardUserDefaults] objectForKey:@"del_arr"] mutableCopy];

for (int i=0; i<del_imgArray.count; i++)

{
if ([[del_imgArray objectAtIndex:i] length] > 0)
{
NSLog(@"delete %d",i);

[assetImages removeObjectAtIndex:i];

UIImageView *thumb = (UIImageView*)[detailScrollView viewWithTag:i+6000];

thumb.image = nil;

}
}

//[self loadThumbs];

}


** Terminating app due to uncaught exception 'NSRangeException', reason: ' -[__NSCFArray removeObjectAtIndex:]: index (3) beyond bounds (2)'

Answer

After taking a long time,me and my colleagues find out what was the exact error and now i'm posting my solution and also explaning why i got the issue.

Error:i have uploading 5 images to the server and also saving locally in documentary path.When i delete the images in editpage it comparing both the server and local,so i got the issue which i mentioned in my question.

Edit Page:(viewwillappear)-Just i need to delete the local images and when update i will delete the everyimages and post in server.So the images will save in server and again i will save it locally and i can delete everyimages.

  #pragma mark - Download Asset Images

 //Asset images -saving locally in documentorypath.

 //img_ids(NSMutableArray)-Taking image id in an array


 -(void)download_Asset_Images
  {
  for (int r = 0; r < assetImages.count; r++)
 {
    NSString *str_id_key = [NSString stringWithFormat:@"image_id%d", r+1];
    NSString *str_id = [NSString stringWithFormat:@"%@", [[assetImages objectAtIndex:r]objectForKey:str_id_key]];
    [img_ids addObject:str_id];

    NSString *str_key = [NSString stringWithFormat:@"main_img%d", r+1];
    NSString *str_asset_img_url =[NSString stringWithFormat:@"%@",[[assetImages objectAtIndex:r]objectForKey:str_key]];
    [self download_img:str_asset_img_url img_name:[NSString stringWithFormat:@"downimg%d.jpg", r]];
}
NSLog(@"%@", img_ids);
}


-(UIImage *)getImageFromURL:(NSString *)fileURL
{
UIImage * result;
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
result = [UIImage imageWithData:data];
return result;
}


-(void)download_img:(NSString *)str_url img_name:(NSString *)str_name
 {
UIImage * imageFromURL = [self getImageFromURL:str_url];
[self saveImagetofolder:imageFromURL imageName:str_name];
}


- (void)saveImagetofolder:(UIImage*)s_image imageName:(NSString*)str_image
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/Asset_images"];
NSString* path = [dataPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", str_image]];
NSData* data = UIImageJPEGRepresentation(s_image, 1.0);
[data writeToFile:path atomically:YES];
 }


#pragma mark - Remove All Images

 -(void)removeallImages
 {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/Asset_images"];

NSArray* files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dataPath error:nil];

for (NSString *filename in files)
{
    [[NSFileManager defaultManager] removeItemAtPath:[dataPath stringByAppendingPathComponent:filename] error:NULL];
}
 }


#pragma mark - Load Thumb Images
 -(void)new_loadThumbs
 {


NSArray *directoryNames = [NSArray arrayWithObjects:@"Asset_images",nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
for (int i = 0; i < [directoryNames count] ; i++)
  {
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:[directoryNames objectAtIndex:i]];
    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil]; //Create folder

    NSArray* files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dataPath error:nil];

    for (int i = 0; i < files.count; i++)
    {
        NSString *imageName=[files objectAtIndex:i];

        NSString *fileNumber = [[imageName componentsSeparatedByCharactersInSet:
                                 [[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
                                componentsJoinedByString:@""];

        NSString* path = [dataPath stringByAppendingPathComponent:imageName];
        UIImage* image = [UIImage imageWithContentsOfFile:path];

        UIImageView *thumb = (UIImageView*)[detailScrollView viewWithTag:[fileNumber intValue]+6000];
        thumb.image = nil;
        thumb.image = image;

        image_tag++;
    }
}
}

pragma mark - Load Asset Images

-(UIImage*)loadAssetImage:(NSString*)image_name
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Asset_images"];
NSString* path = [dataPath stringByAppendingPathComponent:image_name];
UIImage* image = [UIImage imageWithContentsOfFile:path];
return image;
 }