Advanced Stage Nineth: post-processing of photo albums and photos

Source: Internet
Author: User


In the intermediate section, has introduced the album and the simple use of photography, then we will talk about their follow-up treatment, in the end should be how to do it?


First of all, we'll take pictures.

Or the proxy method that was called after the click to use the photo, let's do the things that are mentioned in the following code


 -  (void) Imagepickercontroller: (uiimagepickercontroller *) picker  Didfinishpickingmediawithinfo: (nsdictionary *) info {    [picker  dismissviewcontrolleranimated:yes completion:null];    nsstring *mediatype  = [info valueforkey:uiimagepickercontrollermediatype];    uiimage *newimage ;     ws (weakself);    if  ([mediatype isequaltostring:@ " Public.image "])  {        uiimage *originimage = [ info objectforkey:uiimagepickercontrolleroriginalimage];         Dispatch_sync (Dispatch_get_global_queue (0, 0), ^{             [weakSelf saveImageToPhotos:originImage];         });         newimage = [self scaleimage:originimage toscale:0.1];    }     [_imagearray addobject:newimage];}


At this time, the picture taken from the inside is the original, after all, size, occupy memory is relatively large, we need to cut to optimize it


-(UIImage *) Scaleimage: (UIImage *) image toscale: (float) scalesize {uigraphicsbeginimagecontext (Cgsizemake (    Image.size.width*scalesize,image.size.height*scalesize));    [Image drawinrect:cgrectmake (0, 0, image.size.width * scalesize, Image.size.height *scalesize)];    UIImage *scaledimage = Uigraphicsgetimagefromcurrentimagecontext ();        Uigraphicsendimagecontext (); return scaledimage;}


Of course, we also need to save the original picture in the album, so we Save the method as follows


-(void) Saveimagetophotos: (UIImage *) savedimage {uiimagewritetosavedphotosalbum (savedimage, Self, @selector (image: Didfinishsavingwitherror:contextinfo:), nil);} -(void) Image: (UIImage *) image didfinishsavingwitherror: (nserror *) error ContextInfo: (void *) ContextInfo {nsstring *me    Ssage = nil;    if (error) {message = @ "Save Failed";        } else {message = @ "saved successfully";    [Self getassetslastphoto]; } NSLog (@ "%@", message);}


This is the use of the photo follow-up, if you need to optimize, it depends on your products and how to make plans in the background


Next, let's talk about albums.

The most common is that we need to call a method, all the photos are taken out, it is important to note that we only need thumbnails and the original URL address, because they are still more significant


 -  (void) loadassets {        // initialise     _assets = [nsmutablearray new];    _assetlibrary =  [[alassetslibrary alloc] init];        // run  in the background as it takes a while to get all  Assets from the library    dispatch_async (Dispatch_get_global_queue ( dispatch_queue_priority_default, 0), ^{                 nsmutablearray *assetgroups = [[nsmutablearray alloc]  init];        nsmutablearray *asseturldictionaries =  [[NSMutableArray alloc] init];                 // process assets        void  (^assetEnumerator) ( alasset *, nsuinteger, bool *)  = ^ (alasset *result, nsuinteger  Index, bool *stop)  {            if   (Result != nil)  {                 if  ([[Result valueforproperty:alassetpropertytype] isequaltostring: Alassettypephoto])  {                     [asseturldictionaries addobject:[result valueforproperty: alassetpropertyurls]];                     NSURL *url = result.defaultRepresentation.url;        &nbSp;            [_assetlibrary assetforurl:url                                      resultblock:^ (Alasset *asset)  {                                         if  (Asset)  {                                               @synchronized (_assets)  {                                                 [_assets addObject:asset];                                                  if  (_assets.count == 1)   {                                                     // Added  first asset so reload data                                                      //                                                     [ Self.tableview performselectoronmainthread: @selector (reloaddata)  withobject:nil waituntildone: no];                                                 }                                             }                                         }                                     }                                    failureblock:^ (NSError * Error) {                      &nbSp;                nslog (@ " Operation was not successfull! ");                                    }];                                      }             }         };                //  Process groups        void  (^  Assetgroupenumerator)   (alassetsgroup *, bool *)  = ^ (alassetsgroup *group,&nbsp Bool *stop)  {            if  (group  != nil)  {                 [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:assetEnumerator];                 [assetgroups  addObject:group];            }         };                 // process!        [self.assetlibrary  enumerateGroupsWithTypes:ALAssetsGroupAll                                           usingblock:assetgroupenumerator                                          failureblock:^ (Nserror *error)  {                                             nslog (@ "There is  an error ");                                         }];             });     }


Where thumbnails are displayed, we can display thumbnails, and in the same place we can display the original image based on the URL.


@synchronized (_assets) {Nsmutablearray *copy = [_assets copy]; For (Alasset *asset in copy) {[Photos Addobject:[mwphoto photowiththumbimage:[uiimage imagewithcgimage:asset.t        Humbnail] URL:asset.defaultRepresentation.url]; }    }


The above method is to add the original image thumbnail and address URL, here I used a class containing two attributes to be added, of course you can also get two classes to add separately


In addition, when uploading pictures, you still need to cut the original image, change the base64, of course, depends on your server side of the plan


Okay, here's what we do with the photo album and the post-processing.


Just write it here, what's the problem, you can find me in the group.



This article is from the "Neusoft iOS Alumni Group Technology Blog" blog, please be sure to keep this source http://neusoftios.blog.51cto.com/9977509/1650050

Advanced Stage Nineth: post-processing of photo albums and photos

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.