Beauty image collector (source code + resolution), beauty image collector
Preface:
I haven't written a blog for a while, and the "Persist" badges are hidden. I really shouldn't. The previous paragraph was indeed quite busy, so I just wanted to give myself an excuse to be lazy. My sophomore year is coming to an end, and I have been learning iOS for some time. Today, let's take a moment to open up a function in an App just uploaded a few days ago, such as RT and beauty image collector. Beauty .. I believe no one does not like it. Based on this, this small Demo came into being.
Note:
This article is currently in the blog contest. If it is helpful to you, I hope to vote for it. Thank you.
Voting link: http://vote.blog.csdn.net/Article/Details? Articleid = 37825177 (the voting button is at the bottom)
Demo:
If you are interested in learning, you can first download the source code from my git, and then use the source code to view the analysis below. I believe it will make you gain something.
Git download link: BeautyPickDemo. git
Content:
Source code parsing:
I. First of all, we know that Baidu does not open image APIs to external users, but we can capture the APIS called during access through Google's browser. If you are interested, you can learn about the use of the Google browser Network option, or refer to this article: Baidu image api here, we will mainly introduce how to use it.
1. Baidu image Universal API:Http://image.baidu.com/I? Tn = resultjsonavstar & ie = UTF-8 & word = Andy Lau & pn = 0 & rn = 60
Note:
The return format is json.
Word indicates the query content.
Page number of pn
Rn: number of images returned for one page
Usage: Enter the preceding address in the browser address bar and press enter to view the returned image address.
2. Baidu image classification API (this is what we use)Http://image.baidu.com/channel/listjson? Pn = 0 & rn = 30 & tag1 = beauty & tag2 = All & ie = utf8
Http://image.baidu.com/channel/listjson? Pn = 0 & rn = 30 & tag1 = beauty & tag2 = All & ftags = school Flower & ie = utf8
For others, the results can be obtained according to this method. It is not repeated.
Network Programming is involved in calling APIs. The open-source ASI class library is relatively good (although it is quite old, it has not been updated for a while, but it can meet our needs ). From the source code, you can find the ASI folder of the network request, which contains the required files.
1. Import the files here
2. Required framework for import, including:SystemConfiguration. framework
MobileCoreServices. framework
CFNetwork. framework
Libz. dylib
3. Call the API (see the main interface --> picVC)
@property (nonatomic,strong) ASIHTTPRequest *testRequest;
NSString * urlString = [NSString stringWithFormat: @ "http://image.baidu.com/channel/listjson? Pn = % d & rn = 10 & tag1 = beauty & tag2 = % @ ", nowPage, [chooseArr objectAtIndex: nowChoose]; urlString = [urlString encoding: NSUTF8StringEncoding]; NSURL * url = [NSURL URLWithString: urlString]; testRequest = [ASIHTTPRequest requestWithURL: url]; [testRequest setDelegate: self]; [testRequest startAsynchronous];
You can call the API normally. The following describes how to process the returned data.
II. JSON format
XMLAnd
JSONHere, because the data format returned by calling Baidu image API is JSON, we only need to parse JSON. After the API is called successfully, it will automatically execute this function.
# Pragma mark-Data Loading completed-(void) requestFinished :( ASIHTTPRequest *) request
We only need to parse the data here and use the data.
The data returned by this method is NSData in binary format. We need to manually convert it to UTF8 encoding. You can obtain it as follows:
// NSData * responseData = [request responseData] when reading the returned content in binary; NSString * responseString = [[NSString alloc] initWithData: responseData encoding: NSUTF8StringEncoding];
The next step is a magic time. For such a string, if it is printed directly, you may see that the json format is not rearranged. However, we can use JsonKit for direct parsing. (The file is in the json parsing folder)
You only need such a statement:
self.testDic = [responseString objectFromJSONString];
Print the parsed data as follows:
As for what needs to be taken, just take it directly. For example, we need to obtain the title, url, width, and height of the image.
NSMutableDictionary *nowDic = [[NSMutableDictionary alloc]init];[nowDic setObject:[[array objectAtIndex:i]objectForKey:@"image_url"] forKey:@"image_url"];[nowDic setObject:[[array objectAtIndex:i]objectForKey:@"image_width"] forKey:@"image_width"];[nowDic setObject:[[array objectAtIndex:i]objectForKey:@"image_height"] forKey:@"image_height"];[nowDic setObject:[[array objectAtIndex:i]objectForKey:@"desc"] forKey:@"desc"];[picArray addObject:nowDic];
3. Asynchronous image download + offline Cache
We will use SDWebImage for implementation. For details, see SDWebImage. After parsing json data, we will get the url of the image. You can obtain the image by accessing the url.
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder;
This is a function provided by SDWebImage. by calling it, we can implement asynchronous download and offline cache.
Usage:
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(SPACE / 2 , SPACE / 2 , width, height)];NSURL *url = [NSURL URLWithString:imageInfo.thumbURL];[imageView setImageWithURL:url placeholderImage:nil];imageView.backgroundColor = [UIColor palePurpleColor];[self addSubview:imageView];
Asynchronous download and offline cache effect: (the offline cache can be viewed in the application sandbox)
4. Basic image operations (scaling, deleting, adding, and saving to a local device)
Some common operations are involved here, including scaling, deleting, adding, and saving to a local device.
As for deletion, you can simply add a long-pressed gesture to the image to respond. A dialog box is displayed, prompting you whether to delete the file. After deletion, clear the cache from the sandbox.Add gesture method:
// Long-pressed worker * longRecognizer; longRecognizer = [[using alloc] initWithTarget: self action: @ selector (handleSingleLongFrom :)]; [self addGestureRecognizer: longRecognizer];
Delete from view and sandbox
// Delete [testArr removeObject: data] from the current view; // refresh data _ weak picVC * blockSelf = self; [blockSelf. waterView refreshView: testArr]; [blockSelf. waterView. infiniteScrollingView stopAnimating]; // Delete from sandbox // open sandbox NSArray * paths = cached (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentsDirectory = [paths objectAtIndex: 0]; NSString * namePath = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @ "savedPicInfo _ % d. plist ", nowChoose]; NSMutableArray * picArray = [[NSMutableArray alloc] initWithContentsOfFile: namePath]; for (int I = 0; I <[picArray count]; I ++) {if ([[[picArray objectAtIndex: I] objectForKey: @ "image_url"] isEqualToString: data. thumbURL]) {[picArray removeObjectAtIndex: I]; break;} [picArray writeToFile: namePath atomically: YES];
As for scaling, a full-screen view should be displayed first. Like this:
// Click to display the big image-(void) showImage :( ImageInfo *) data {NSURL * url = [NSURL URLWithString: data. thumbURL]; [clickImage setImageWithURL: url placeholderImage: nil]; TGRImageViewController * viewController = [[TGRImageViewController alloc] initWithImage: clickImage. image setImageInfo: data]; viewController. transitioningDelegate = self; [self presentViewController: viewController animated: YES completion: nil];}
The essence is to call presentViewController: viewController. Of course, we can add an animated effect to the display of the new view, as shown below:
#pragma mark - UIViewControllerTransitioningDelegate methods- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{ if ([presented isKindOfClass:TGRImageViewController.class]) { return [[TGRImageZoomAnimationController alloc] initWithReferenceImageView:clickImage]; } return nil;}- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed { if ([dismissed isKindOfClass:TGRImageViewController.class]) { return [[TGRImageZoomAnimationController alloc] initWithReferenceImageView:clickImage]; } return nil;}
Then, in the new view, add, click, and remove, and then press and hold to display a new operation. You can move the double-finger zoom gesture. The specific implementation is as follows:
#pragma mark - Private methods- (void)longPress:(UITapGestureRecognizer *)tapGestureRecognizer{ if(tapGestureRecognizer.state == UIGestureRecognizerStateBegan) { [self popupActionSheet]; }}- (IBAction)handleSingleTap:(UITapGestureRecognizer *)tapGestureRecognizer { [self dismissViewControllerAnimated:YES completion:nil];}- (IBAction)handleDoubleTap:(UITapGestureRecognizer *)tapGestureRecognizer { if (self.scrollView.zoomScale == self.scrollView.minimumZoomScale) { // Zoom in CGPoint center = [tapGestureRecognizer locationInView:self.scrollView]; CGSize size = CGSizeMake(self.scrollView.bounds.size.width / self.scrollView.maximumZoomScale, self.scrollView.bounds.size.height / self.scrollView.maximumZoomScale); CGRect rect = CGRectMake(center.x - (size.width / 2.0), center.y - (size.height / 2.0), size.width, size.height); [self.scrollView zoomToRect:rect animated:YES]; } else { // Zoom out [self.scrollView zoomToRect:self.scrollView.bounds animated:YES]; }}
5. Pull-down refresh. This function is used to browse images. The code is in picVC. However, I have previously written such a blog. It will not be repeated. For details, see here:
IOS development-iOS 7 pull-down refresh, loading on quick integration
Sat. As the name suggests, slide show is capable of automatically playing the favorite beautiful pictures .. the principle here is to use the UIView animation to continuously switch between the display pictures and the display effect.
The switchover effect is as follows:
_transitionOptions= @[[NSNumber numberWithInteger:UIViewAnimationOptionTransitionFlipFromLeft], [NSNumber numberWithInteger:UIViewAnimationOptionTransitionFlipFromRight], [NSNumber numberWithInteger:UIViewAnimationOptionTransitionCurlUp], [NSNumber numberWithInteger:UIViewAnimationOptionTransitionCurlDown], [NSNumber numberWithInteger:UIViewAnimationOptionTransitionCrossDissolve], [NSNumber numberWithInteger:UIViewAnimationOptionTransitionFlipFromTop], [NSNumber numberWithInteger:UIViewAnimationCurveEaseIn], [NSNumber numberWithInteger:UIViewAnimationCurveEaseOut], [NSNumber numberWithInteger:UIViewAnimationCurveLinear], [NSNumber numberWithInteger:UIViewAnimationOptionAllowAnimatedContent], [NSNumber numberWithInteger:UIViewAnimationOptionOverrideInheritedCurve], [NSNumber numberWithInteger:UIViewAnimationOptionTransitionFlipFromTop], [NSNumber numberWithInteger:UIViewAnimationOptionTransitionFlipFromBottom]];
When switching the image, implement the following code. (For details, see PhotoStackView)
-(void)reloadData { if (!self.dataSource) { //exit if data source has not been set up yet self.photoViews = nil; return; } NSInteger numberOfPhotos = [self.dataSource numberOfPhotosInPhotoStackView:self]; NSInteger topPhotoIndex = [self indexOfTopPhoto]; // Keeping track of current photo's top index so that it remains on top if new photos are added if(numberOfPhotos > 0) { NSMutableArray *photoViewsMutable = [[NSMutableArray alloc] initWithCapacity:numberOfPhotos]; UIImage *borderImage = [self.borderImage resizableImageWithCapInsets:UIEdgeInsetsMake(self.borderWidth, self.borderWidth, self.borderWidth, self.borderWidth)]; for (NSUInteger index = 0; index < numberOfPhotos; index++) { UIImage *image = [self.dataSource photoStackView:self photoForIndex:index]; CGSize imageSize = image.size; if([self.dataSource respondsToSelector:@selector(photoStackView:photoSizeForIndex:)]){ imageSize = [self.dataSource photoStackView:self photoSizeForIndex:index]; } UIImageView *photoImageView = [[UIImageView alloc] initWithFrame:(CGRect){CGPointZero, imageSize}]; photoImageView.image = image; UIView *view = [[UIView alloc] initWithFrame:photoImageView.frame]; view.layer.rasterizationScale = [[UIScreen mainScreen] scale]; view.layer.shouldRasterize = YES; // rasterize the view for faster drawing and smooth edges if (self.showBorder) { // Add the background image if (borderImage) { // If there is a border image, we need to add a background image view, and add some padding around the photo for the border CGRect photoFrame = photoImageView.frame; photoFrame.origin = CGPointMake(self.borderWidth, self.borderWidth); photoImageView.frame = photoFrame; view.frame = CGRectMake(0, 0, photoImageView.frame.size.width+(self.borderWidth*2), photoImageView.frame.size.height+(self.borderWidth*2)); UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:view.frame]; backgroundImageView.image = borderImage; [view addSubview:backgroundImageView]; } else { // if there is no boarder image draw one with the CALayer view.layer.borderWidth = self.borderWidth; view.layer.borderColor = [[UIColor whiteColor] CGColor]; view.layer.shadowOffset = CGSizeMake(0, 0); view.layer.shadowOpacity = 0.5; } } [view addSubview:photoImageView]; view.tag = index; view.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds)); [photoViewsMutable addObject:view]; } // Photo views are added to subview in the photoView setter self.photoViews = photoViewsMutable; photoViewsMutable = nil; [self goToPhotoAtIndex:topPhotoIndex]; } }
7. The custom image display function in the background shows the effect. When the application switches to the background, double-click the home Key to display the background program. For example, sometimes the image we browse is relatively large, and we want to hide it when we switch to the background ..
This involves the application of Background Fetch. I have also written a special blog introduction. We will not repeat it here. For details, see:
IOS development-custom background display pictures (iOS7-Background Fetch Application)
Okay. At last, it is about to be introduced.
Of course. I have summarized the analysis here, listing several key code segments.
For more details, you need to view the code yourself. The annotations are also written. It is estimated that there is no problem. If you have any questions, contact me.
I wrote my blog for three hours in one breath. I also hope it will help you.
This article is currently in the blog contest. If it is helpful to you, I hope to vote for it. Thank you.
Voting link: http://vote.blog.csdn.net/Article/Details? Articleid = 37825177 (the voting button is at the bottom)
On the way to learning, I will share with you.
It is best to find a beauty picture station website source code with data
Well, the above people are asking for money to buy, and I am also buying, a set of very good picture website source code, but I can send it to you for free!
Beautiful pictures site source code, high score for source code
Tube8