5

मेरा ऐप आईफोन की एल्बम सूची और कुछ एल्बम में सभी तस्वीरें प्राप्त करना चाहता है।जीसीडी और केवीओ समस्याएं

ऐप में मैं आईफोन के एक एल्बम में फोटो का आकलन करता हूं। प्रदर्शन के विचार पर, कुछ एल्बम की बहुत सारी तस्वीरें हो सकती हैं, मैं जीसीडी का उपयोग करता हूं: dispatch_async। लेकिन यह हमेशा क्रैश होता है जब केवीओ द्वारा टेबलव्यू सेल अपडेट का आह्वान किया जाता है। मुझे इस बारे में कोई जानकारी नहीं है कि मैं गलत तरीके से केवीओ या जीसीडी का उपयोग करता हूं या नहीं।

वैकल्पिक रूप से मैं performSelectorInBackground का उपयोग करता हूं: dispatch_async की जगह। अब ऐप क्रैश नहीं हुआ है लेकिन ऐप का प्रदर्शन खराब है: सेल का शीर्षक केवल तब दिखाया जाएगा जब आप इसे स्पर्श करते हैं या कई फ़ोटो होने पर टेबलव्यू स्क्रॉल करते हैं। दूसरे शब्दों में, मुख्य धागा अवरुद्ध होना चाहिए।

संलग्न कोड है और कोर कोड एल्बम लिस्ट व्यू कंट्रोलर.एम में है।

क्या कोई इसे जांचने में मेरी सहायता कर सकता है?

मैं सिर्फ जानना चाहता हूँ: 1 क्यों एप्लिकेशन यदि dispatch_async 2 का उपयोग कर दुर्घटनाग्रस्त हो गया है कि कैसे मैं कई फोटो के मामले में प्रदर्शन में सुधार कर सकते हैं।

धन्यवाद।

 
// 
// RootViewController.h 
// AlbumDemo 


#import 

@interface RootViewController : UITableViewController { 
    NSMutableArray *_listArray; 
} 

@property (nonatomic, retain) NSMutableArray *listArray; 

@end 


// RootViewController.m 


#import "RootViewController.h" 
#import 
#import "AlbumListViewController.h" 
NSString *thumnail = @"thumnail"; 
NSString *albumName = @"albumName"; 
NSString *albumNum = @"albumNum"; 
NSString *albumGroup = @"albumGroup"; 
@implementation RootViewController 
@synthesize listArray = _listArray; 

#pragma - 
#pragma Function 
- (void)setUp 
{ 
    _listArray = [[NSMutableArray alloc] initWithCapacity:1]; 
    self.title = @"Albums"; 
} 
- (void)fetchAlbumList 
{ 
    ALAssetsLibrary *assetLib = [[[ALAssetsLibrary alloc] init] autorelease]; 
    ALAssetsFilter *fileter = [ALAssetsFilter allPhotos]; 
    [assetLib enumerateGroupsWithTypes:ALAssetsGroupAll 
          usingBlock:^(ALAssetsGroup *group, BOOL *stop) 
    { 
     if (group) 
     { 
      [group setAssetsFilter:fileter]; 
      NSString *_groupName = [group valueForProperty:ALAssetsGroupPropertyName]; 
      NSNumber *_groupNum = [NSNumber numberWithInteger:[group numberOfAssets]]; 
      UIImage *_groupImage = [UIImage imageWithCGImage:[group posterImage]]; 

      NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:_groupName,albumName,_groupNum,albumNum,_groupImage,thumnail,group,albumGroup, nil]; 

      [_listArray addObject:dic]; 
      [self.tableView reloadData]; 

     } 
     else 
     { 
      NSLog(@"_listArray :%@",_listArray); 
     } 

    } 
          failureBlock:^(NSError *error) 
    { 
     NSLog(@"Error: %@", error);; 
    } 
    ]; 

} 
#pragma - 
#pragma ViewController lift cycle 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self setUp]; 
    [self fetchAlbumList]; 

} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
    [super viewDidDisappear:animated]; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 50; 
} 
// Customize the number of sections in the table view. 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [_listArray count]; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UILabel *nameLab = nil; 
    UILabel *numLab = nil; 
    UIImageView *thumnailImage = nil; 

    UIFont *font = [UIFont boldSystemFontOfSize:18]; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

     thumnailImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,50, 50)]; 
     thumnailImage.tag = 100; 
     [cell.contentView addSubview:thumnailImage]; 
     [thumnailImage release]; 


     nameLab = [[UILabel alloc] initWithFrame:CGRectMake(60, 10, 100, 30)]; 
     nameLab.tag = 200; 
     nameLab.backgroundColor = [UIColor clearColor]; 
     nameLab.font = font; 
     [cell.contentView addSubview:nameLab]; 
     [nameLab release]; 

     numLab = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 50, 30)]; 
     numLab.tag = 300; 
     numLab.backgroundColor = [UIColor clearColor]; 
     numLab.textColor = [UIColor grayColor]; 
     numLab.font = font; 
     [cell.contentView addSubview:numLab]; 
     [numLab release]; 
    } 
    else 
    { 
     thumnailImage = (UIImageView *)[cell.contentView viewWithTag:100]; 
     nameLab = (UILabel *)[cell.contentView viewWithTag:200]; 
     numLab = (UILabel *)[cell.contentView viewWithTag:300]; 
    } 

    NSDictionary *dic = [self.listArray objectAtIndex:indexPath.row]; 

    thumnailImage.image = (UIImage *)[dic valueForKey:thumnail]; 

    NSString *title = [dic valueForKey:albumName]; 
    CGSize titleSize = [title sizeWithFont:font]; 
    CGRect rect = nameLab.frame; 
    rect.size = titleSize; 
    nameLab.frame = rect; 
    nameLab.text = title; 

    rect = numLab.frame; 
    rect.origin.x = 60 + nameLab.frame.size.width + 10; 
    numLab.frame = rect; 

    numLab.text = [NSString stringWithFormat:@"(%d)",[[dic valueForKey:albumNum] intValue]]; 


    // Configure the cell. 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSDictionary *dic = [self.listArray objectAtIndex:indexPath.row]; 

    AlbumListViewController *viewController = [[AlbumListViewController alloc] initWithAssetGroup:[dic valueForKey:albumGroup]]; 
    [self.navigationController pushViewController:viewController animated:YES]; 
    [viewController release]; 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Relinquish ownership any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 

    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand. 
    // For example: self.myOutlet = nil; 
} 

- (void)dealloc 
{ 
    My_Release (_listArray); 
    [super dealloc]; 
} 

@end 


// AlbumListViewController.h 
// AlbumDemo 

#import 
#import 

@interface AlbumListViewController : UITableViewController { 
    NSMutableArray *_marr; 
    ALAssetsGroup *_assetsGroup; 
} 

@property (nonatomic, retain) NSMutableArray *list; 
@property (nonatomic, retain) ALAssetsGroup *assetsGroup; 

- (id)initWithAssetGroup:(ALAssetsGroup *)group; 
@end 

// AlbumListViewController.m 
// AlbumDemo 

#import "AlbumListViewController.h" 

@interface PhotoObj : NSObject { 
    NSString *_name; 
    UIImage *_thumbnail; 
    UIImage *_fullImage; 
} 

@property (nonatomic, copy ) NSString *name; 
@property (nonatomic, retain) UIImage *thumbnail; 
@property (nonatomic, retain) UIImage *fullImage; 
@end 

@implementation PhotoObj 
@synthesize name = _name; 
@synthesize thumbnail = _thumbnail,fullImage = _fullImage; 
- (void)dealloc 
{ 
    My_Release(_thumbnail); 
    My_Release(_fullImage); 
    My_Release(_name); 
    [super dealloc]; 
} 
@end 

@interface AlbumListViewController() 

- (NSMutableArray*)list; 
- (NSUInteger)countOfList; 
- (id)objectInListAtIndex:(NSUInteger)idx; 
- (void)insertObject:(id)anObject inListAtIndex:(NSUInteger)idx; 
- (id)objectInListAtIndex:(NSUInteger)idx; 
- (void)removeObjectFromListAtIndex:(NSUInteger)idx; 
- (void)replaceObjectInListAtIndex:(NSUInteger)idx withObject:(id)anObject; 
- (void)setList:(NSMutableArray *)_arr; 

@end 

@implementation AlbumListViewController 
@synthesize assetsGroup = _assetsGroup; 

- (id)initWithAssetGroup:(ALAssetsGroup *)group 
{ 
    self = [self initWithStyle:UITableViewStylePlain]; 
    if (self) 
    { 
     _marr = [[NSMutableArray alloc] initWithCapacity:1]; 
     self.assetsGroup = group; 
     self.tableView.delegate = self; 
     self.tableView.dataSource = self; 

    } 
    return self; 
} 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)dealloc 
{ 
    My_Release(_marr); 
    My_Release(_assetsGroup); 
    [self removeObserver:self forKeyPath:@"list"]; 
    [super dealloc]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

} 

#pragma mark - View lifecycle 
- (void)parseAssetGroup 
{ 
    [_marr removeAllObjects]; 
    [self.assetsGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) { 
     if (result) 
     { 
      PhotoObj *obj = [[PhotoObj alloc] init]; 
      obj.thumbnail = [UIImage imageWithCGImage:[result thumbnail]]; 
      ALAssetRepresentation *represention = [result defaultRepresentation]; 
      obj.fullImage = [UIImage imageWithCGImage:[represention fullScreenImage]]; 
      obj.name = [[represention url] absoluteString]; 


      [self willChangeValueForKey:@"list"]; 
      [self insertObject:obj inListAtIndex:[_marr count]]; 
      [self didChangeValueForKey:@"list"]; 
      My_Release(obj); 
     } 

    }]; 

} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self addObserver:self forKeyPath:@"list" options:NSKeyValueObservingOptionNew |NSKeyValueObservingOptionOld context:NULL]; 
    /* 
    if performSelectorInBackground, the perofrmance is poor 
    as the title of the cell will be shown in a long time and it now seems the main thread is blocked 
    */ 
    [self performSelectorInBackground:@selector(parseAssetGroup) withObject:nil]; 
    /* 
    using dispatch_async it always crashes 
    as it says the sth is wrong with the tableview update 

    */ 

// dispatch_async(dispatch_get_main_queue(), ^{ 
//  [self parseAssetGroup]; 
// }); 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 

} 

#pragma mark - Table view data source 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 50; 
} 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [_marr count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UIImageView *thumbNail = nil; 
    UILabel *nameLab = nil; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

     thumbNail = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; 
     thumbNail.tag = 99; 
     [cell.contentView addSubview:thumbNail]; 
     [thumbNail release]; 

     nameLab = [[UILabel alloc] initWithFrame:CGRectMake(60, 10, 240, 40)]; 
     nameLab.numberOfLines = 2; 
     nameLab.font = [UIFont systemFontOfSize:16]; 
     nameLab.tag = 199; 
     [cell.contentView addSubview:nameLab]; 
     [nameLab release]; 
    } 
    else 
    { 
     thumbNail = (UIImageView *)[cell.contentView viewWithTag:99]; 
     nameLab = (UILabel *)[cell.contentView viewWithTag:199]; 
    } 
    // Configure the cell... 
    PhotoObj *obj = [_marr objectAtIndex:indexPath.row]; 
    nameLab.text = obj.name; 
    thumbNail.image = obj.thumbnail; 

    return cell; 
} 
#pragma mark - 
- (NSUInteger)countOfList 
{ 
    return [_marr count]; 
} 
- (NSMutableArray*)list 
{ 
    return _marr; 
} 
- (void)setList:(NSMutableArray *)_arr 
{ 
    if (_marr != _arr) 
    { 
     [_marr release]; 
     _marr = _arr; 
    } 
} 

- (id)objectInListAtIndex:(NSUInteger)idx 
{ 
    return [_marr objectAtIndex:idx]; 
} 

- (void)insertObject:(id)anObject inListAtIndex:(NSUInteger)idx 
{ 
    if ([NSThread isMainThread]) 
    { 
     NSLog(@"insert main thread"); 
    } 
    else 
    { 
     NSLog(@"insert not main thread"); 
    } 
    [_marr insertObject:anObject atIndex:idx]; 
} 


- (void)removeObjectFromListAtIndex:(NSUInteger)idx 
{ 
    [_marr removeObjectAtIndex:idx]; 
} 
- (void)replaceObjectInListAtIndex:(NSUInteger)idx withObject:(id)anObject 
{ 
    [_marr replaceObjectAtIndex:idx withObject:anObject]; 
} 
- (void)observeValueForKeyPath:(NSString *)keyPath 
         ofObject:(id)object 
         change:(NSDictionary *)change 
         context:(void *)context 
{ 
    NSIndexSet *indices = [change objectForKey:NSKeyValueChangeIndexesKey]; 
    if (indices == nil) 
     return; // Nothing to do 

    // Build index paths from index sets 
    NSUInteger indexCount = [indices count]; 
    NSUInteger buffer[indexCount]; 
    [indices getIndexes:buffer maxCount:indexCount inIndexRange:nil]; 

    NSMutableArray *indexPathArray = [NSMutableArray array]; 
    for (int i = 0; i

उत्तर

7

मैं आज बिल्कुल एक ही समस्या में पड़ गए:

नीचे मेरी कोड है। संक्षेप में, कारण यह है कि आप पृष्ठभूमि प्रेषण कतार से UIKit संबंधित कार्यों, जैसे तालिका को अपडेट करना, या मेरे मामले में टेक्स्टव्यू नहीं कर सकते हैं। अधिक जानकारी के लिए नीचे दिए गए लिंक को देखें।

comparison GCD vs. performSelectorInBackground: dispatch_async not in background

सम्भावित समाधान पीछा कर रहा है: के बजाय सीधे KVO चर जो दुर्घटना का कारण बनता है के लिए अपने अद्यतन ब्लॉक में अपने ताजा डेटा देने की है, तो आप एक और ब्लॉक जो मुख्य कतार को यह करता प्रेषण, अपने अपडेट ब्लॉक के अंदर से। यदि आप ऐसा करने के लिए dispatch_async_f फ़ंक्शन का उपयोग करते हैं, तो आप संदर्भ के रूप में अपने डेटा में एक पॉइंटर पास कर सकते हैं।

इस तरह:

dispatch_async(yourQueue, ^() { 
    NSArray *data; 
    // do stuff to alloc and fill the array 
    // ... 
    dispatch_async(dispatch_get_main_queue(), ^() { 
    myObj.data = data; // the assignment, which triggers the KVO. 
    }); 
}); 

मेरे लिए इस बनाए रखने और डेटा को रिहा बिना काम करता है। यकीन नहीं है, अगर यह सही है।

+0

धन्यवाद, मैं एक नज़र डालेगा। – scorpiozj

+0

क्या यह काम करता है? फिर आप – tomk

+0

के उत्तर के रूप में प्रश्न को चिह्नित कर सकते हैं, यह मुख्य/पृष्ठभूमि धागे से कोई फर्क नहीं पड़ता क्योंकि मैंने अन्य ऐप में उसी तरह से किया था और यह अच्छी तरह से काम करता है। इसलिए मुझे अभी भी पता नहीं है कि समस्या क्या है। – scorpiozj