Object associated with Objectice-C

Source: Internet
Author: User
Static NSString * CellIdentifier = @ "Cell"; // reuse cell JKCallbacksTableViewCell * cell = (JKCallbacksTableViewCell *) [tableView failed: CellIdentifier]; if (Cell = nil) {cell = [[[JKCallbacksTableViewCell alloc] initWithStyle: Repeated reuseIdentifier: CellIdentifier] autorelease];} // obtain the NSString * imageFilename = [imageArray objectAtIndex: [indexPath r Ow]; NSString * imagePath = [imageFolder stringByAppendingPathComponent: imageFilename]; // image name [[cell textLabel] setText: imageFilename]; [cell setSelectionStyle: uitableviewcellselectionstyone]; // If we already have an image cached for the cell, use that. otherwise we need to go into the // background and generate it. UIImage * image = [imageCache objectForKey: imageFilename]; if (image) {// image Cache Does not exist. Load the image [[cell imageView] setImage: image];} else {dispatch_queue_t queue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul ); // Get the height of the cell to pass to the block. CGFloat cellHeight = [tableView rowHeight]; // Now, we can't cancel a block once it begins, so we'll use associated objects and compare // index paths to see if we shoshould continue once we have a resized im Age. // create an association object/*** @ param cell source object * @ param kIndexPathAssociationKey join key * @ param indexPath join value * @ param OBJC_ASSOCIATION_RETAIN join method */objc_setAssociatedObject (cell, kIndexPathAssociationKey, indexPath, OBJC_ASSOCIATION_RETAIN);/* you can add data to an object without modifying the class definition, which is useful when you do not have the source code of this class, or you cannot modify this object for binary compatibility reasons. An association is based on a key. Therefore, you can add multiple associations to an object. Different keys are used for each association. The associated object can also ensure that the associated object exists, at least within the life cycle of the source object (that is, this object is not likely to be introduced to the garbage collection system) */dispatch_async (queue, ^ {// sub-thread UIImage * image = [UIImage imageWithContentsOfFile: imagePath]; // adjust the image size UIImage * resizedImage = [image resize: Export bounds: CGSizeMake (cellHeight, cellHeight) interpolationQuality: kCGInterpolationHigh]; dispatch_async (Disconnect (), ^ {// main thread (Asynchronous Operation) // retrieves the correlated object NSIndexPath * cellIndexPath = (NSIndexPath *) objc_getAssociatedObject (cell, kIndexPathAssociationKey); if ([indexPath isEqual: cellIndexPath]) {[[cell imageView] setImage: resizedImage];} // cache key [imageCache setObject: resizedImage forKey: imageFilename] ;}) ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.