How to Learn new frameworks (save images to albums) and frame albums

Source: Internet
Author: User

How to Learn new frameworks (save images to albums) and frame albums

1. Click View big image 1. Click the image or button (Click View big image), modal a controller to display the large image 2. How can I solve this problem so that I can click the picture to view the big image? Two methods: 1. add a point-and-click gesture to the image. 2. add-(void) touchesEnded :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event 2.1 Add point by gesture to view where the image is located. Advantages: applicable to any scenario, unrestricted disadvantages: a large amount of code is 2.2. Add touchesEnded to the view where the image is located. Method advantages: Click to view the large image quickly. The disadvantage is that the amount of code is small: the method can be used only when the view where the image is located is as big as the image. The application scenario is limited. how can I quickly get the Controller to modal? The controller that can be obtained quickly is the window root controller 4. create a controller. When you click the image, modal the controller out 4.1 and add a scrollView to the Controller, occupy full screen because the large image can scroll up or down to view the 4.2 scrollView and add a UIImageView. We need to manually create a UIImageView and add it to the scrollView manually because the image size is not fixed and the large image occupies full screen, in the center of the thumbnail 4.3, add two buttons to the Controller, one is to save the image button 4.4, so the controller we created should be UIViewController 5. the controller that comes out of moadl displays the big picture. 5.1 if you want to view the big picture in the controller, you must first get the big picture. How can you get it? 5.1.1 you can create a notification and pass the image as a notification parameter. 5.1.2 because the controller is modal in the uiview of the image, you can pass the model to the Controller in the following method: 1. get the controller of the next level at the upper level 2. define properties in the next level controller to receive data 3. in the previous level, assign a value to the attributes defined in the next level. 5.2 create a UIImageView control using code in the view large chart controller, and add biang to scrollView. 5.3 check whether a large image is 5.3.1 or a large image, set the frame of UIImageView to display the width equal to the screen width starting from 0 to 0 (coordinate origin), and scale the height according to the zoom ratio of the width, set the center of UIImageView to the center of the screen. 6. how can I scale a large image by 6.1 when it is displayed? 6.1.1 Add a kneading gesture and use transform for image scaling advantages: applicable to any scenario disadvantages: complicated code 6.1.2 use scrollView proxy method advantages: the code is relatively simple disadvantages: you can only use 6.2 to use scrollView proxy. zoom 6.2.1 follow the protocol to implement proxy method 6.2.2 return the view 6.2.3 to be scaled in the proxy method to view the large image and zoom the source code at the maximum (minimum scale) of the outside world.
1-(void) viewDidLoad {2 [super viewDidLoad]; 3 4 UIImage * image = [[SDImageCache nvidimagecache] imageFromDiskCacheForKey: _ item. image0]; 5 6 UIImageView * imageView = [[UIImageView alloc] init]; 7 _ imageView = imageView; 8 [imageView sd_setImageWithURL: [NSURL URLWithString: _ item. image1] placeholderImage: image]; 9 CGFloat h = XMGScreenW/_ item. width * _ item. height; 10 imageView. frame = CGRectMake (0, 0, XMGScreenW, h); 11 [_ scroolView addSubview: imageView]; 12 13 if (_ item. is_bigPicture) {14 _ scroolView. contentSize = CGSizeMake (0, h); 15 16 if (_ item. height> h) {17 CGFloat scale = _ item. height/h; 18 // 2. set the zoom ratio to 19 _ scroolView. maximumZoomScale = scale; 20 _ scroolView. minimumZoomScale = 1; 21} 22} else {23 imageView. center = CGPointMake (XMGScreenW * 0.5, XMGScreenH * 0.5); 24} 25 // zoom 26 // 1. set proxy to tell it which View needs to be scaled 27 // 2. set the scaling ratio to 28_scroolview. delegate = self; 29} 30 # pragma mark-UIScrollViewDelegate31 // function: return the view32 to be scaled // call: 33-(UIView *) viewForZoomingInScrollView :( UIScrollView *) scrollView34 {35 return _ imageView; 36}

 

II. save the image to the album. there are two ways to save an image to an album. 1.1 call the system method to save it to an album. Advantages: simple and quick. Two methods can achieve the disadvantages: they can only be saved to a system album, it cannot be saved to a custom album. Note: The method used to monitor whether an image is saved must be specified by the system, you can jump into the method of saving images to the album to view this method. 1.2 use the Photos framework to customize the album to save images. Advantages: saving images to the specified album. Disadvantages: Large diamante volume, you need to learn how to save the new framework to the source code of the system album.
1 // click save to call 2-(IBAction) save :( id) sender {3 // save the system album 4 UIImageWriteToSavedPhotosAlbum (_ imageView. image, self, @ selector (image: didFinishSavingWithError: contextInfo :), nil ); 5} 6 // ask whether the current app is allowed to access the album. 7 // check whether the listening image is saved. 8-(void) image :( UIImage *) image didFinishSavingWithError :( NSError *) error contextInfo :( void *) contextInfo 9 {10 if (error) {11 [SVProgressHUD showErrorWithStatus: @ "failed to save"]; 12} else {13 [SVProgressHUD showSuccessWithStatus: @ "saved successfully"]; 14} 15}

 

2. How to learn a new framework 2.1 first, how to find out the classes commonly used in this framework 2.2, and what classes are commonly used? 2.2.1 Baidu (du Niang of tianchao is too complex and changeable) 2.2.2 view official documentation 2.3 How can I quickly locate the official documentation of the relevant framework? Search Keyword: FrameWork name + FrameWork Reference

 

 

2.4 how to learn how to use a class (method )? Hold down option and click the class name or method name to sum up: 1. If you want to know the class, what are the items, skip the header file 2. If you want to know how to use it, use option

 

3. using the Photos framework, custom album save picture 3.1 save picture to album principle 3.1.1 create new album 3.1.2 save picture to system album 3.1.3 copy picture from system album to new album Photos 5 categories, operation photo album
When you own
PHPhotoLibrary: album: (all album sets)
PHAssetCollection: album (image set)
PHAsset: Image
 
When creating:
PHAssetCreationRequest: create a new image, delete an image, modify an image PHAssetCollectionChangeRequest: Create an album, delete an album, and modify album 3.2. How do I save an image to my own album? Use the Photos framework 3.3 Photos framework to save images to your own album step 3.3.1 create an image request class (create a new image for the system album) 3.3.2 create an album request class (modify the album) 3.3.3 copy an image to your own album 3.3.4 step 3.3.5 in the class introduction example 3.3.5 note: The image creation and album request classes must be packaged in a method, which can be viewed in the header file, this method is divided into two types: synchronous and asynchronous. How do I copy an image to my album using asynchronous 3.4? 3.4.1 what is the meaning of calling the [assetCollectionChangeRequest addAssets: <# (nonnull id <NSFastEnumeration>) #>] method 3.4.2 (nonnull id <NSFastEnumeration>) parameter with an album request object? It indicates that an object can be uploaded, but the NSFastEnumeration protocol is followed by an array. Because Enumeration indicates traversal, the array can be traversed. 3.4.3. I don't know how to write the parameter. What should I do? Press "option" and click "method" to view the Method Introduction. This section describes how to write the parameters 3.4.4. After you see this NSFastEnumeration protocol, you can see Array 4. after the functions are completed, we also found two problems: 4.1 check whether the app has the permission to access the album before saving (if not authorized, ask the user if they are allowed to access the album) 4.2 A new album will be created each time it is saved. 5. permission Problem Solution 5.1 check the app permissions before saving each time. If access is allowed, directly save 5.2 and do not allow access, the system prompts the user to set the permission in 5.3. If no permission is granted, the permission dialog box pops up to allow users to grant permissions in four states: (the first time) PHAuthorizationStatusNotDetermined = 0. you are not sure whether to allow the current app to access the system album.
PHAuthorizationStatusRestricted, parental control
PHAuthorizationStatusDenied: The current app is not allowed to access the system album PHAuthorizationStatusAuthorized (authorized). 6. The current app is allowed to access the system album. each time you create a new album solution 6.1, before saving, first check whether there is an album 6.2 with the same name in the system album. If the album 6.3 exists, save it to this album does not exist, and then create an album to save 7. how can I determine whether an album exists? 7.1 think: Can an attribute be used to determine whether an album exists? No. The next time the launch attribute is empty, can 7.2 store the attribute in the sandbox? No. If the app is uninstalled and re-installed, the sandbox is empty, but there is a 7.3 Final Solution in the album: Get all albums and check whether there is an album of the same name 8. How to get all albums? 8.1 check whether the album class has such a method to find the keyword PHAssetCollection album. No 8.2 is found to check whether the album class has such a method. You may find a method for managing the album yourself + (PHFetchResult <PHAssetCollection *> *) fetchAssetCollectionsWithType :( PHAssetCollectionType) type subtype :( PHAssetCollectionSubtype) subtype options :( nullable PHFetchOptions *) options; fetch lookup means 8.3 what is the return value type of PHFetchResult? Click it to check whether the 8.4 parameter is not passed. You can use option or leave it blank first. If there is any problem, solve the parameter problem when you come back. If it is an enumeration parameter, generally, the default value is saved to the source code of the custom album.
1 // click save call 2-(IBAction) save :( id) sender {3 // view the current app authorization status 4 PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus]; 5 switch (status) {6 case PHAuthorizationStatusNotDetermined: 7 {// unauthorized, pop-up authorization box 8 [PHPhotoLibrary requestAuthorization: ^ (PHAuthorizationStatus status) {9 // After the user selects it, it will be called-select allow, save 10 if (status = PHAuthorizationStatusAuthorized) {11 [self savePhoto]; 12} 13}]; 14 if no access is allowed, 15 break is not saved; 16} 17 case PHAuthorizationStatusAuthorized: 18 {// authorization, you can directly save 19 [self savePhoto]; 20 break; 21} 22 default: 23 {// refuse to tell the user where to open authorization 24 [SVProgressHUD showInfoWithStatus: @ "enable settings-> search for best friend-> enable the photo switch-> allow the current app to access the system album to save the image"]; 25 break; 26} 27} 28} 29 30 // Add an image to your album 31-(void) savePhoto32 {33 [[PHPhotoLibrary sharedPhotoLibrary] ~mchanges: ^ {34 // 1. create an image request class (create a new image in the system album) PHAssetCreationRequest35 // put the image in the system album 36 PHAssetCreationRequest * assetCreationRequest = [PHAssetCreationRequest creationRequestForAssetFromImage: _ imageView. image]; 37 38 // 2. create an album request class (modify album) Export PHAssetCollectionChangeRequest * assetCollectionChangeRequest = nil; 40 41 // obtain the previous album 42 PHAssetCollection * assetCollection = [self fetchAssetCollection: @ "best friend"]; 43 44 // determine whether an album exists 45 if (assetCollection) {46 // if an existing album with the same name already exists, specify this album, and create an album request to modify the class 47 assetcollectionchangerequestforassetcollection: assetCollection]; 48} else {// creation of new album 49 assetCollectionChangeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle: @ "best friend"]; 50} 51/3. add the image to the album. 52 // NSFastEnumeration: If you see this in the future, the array is 53 // assetCreationRequest. placeholderForCreatedAsset image request class placeholder (equivalent to a memory address) 54 // because the callback method is implemented asynchronously, it cannot be ensured that assetCreationRequest has a value of 55 56 [assetCollectionChangeRequest addAssets: @ [assetCreationRequest. success]; 57 58} completionHandler: ^ (BOOL success, NSError * _ Nullable error) {59 60 if (success) {61 [SVProgressHUD showSuccessWithStatus: @ "saved successfully"]; 62} else {63 [SVProgressHUD showErrorWithStatus: @ "failed to save"]; 64} 65 66}]; 67} 68 69 // specify the album name, get album 70-(PHAssetCollection *) fetchAssetCollection :( NSString *) title71 {72 // get all custom albums in the album 73 PHFetchResult * result = [PHAssetCollection fetchAssetCollectionsWithType: Prepare subtype: prepare options: nil]; 74 traverse the album and determine whether an album with the same name exists. 75 for (PHAssetCollection * assetCollection in result) {76 if ([title isw.tostring: assetCollection. localizedTitle]) {If yes, the 77 return assetCollection; 78} 79} 80 return nil; 81} of the album will be returned}

 

Related Article

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.