Uiimageview, uiimage, cgcontextref

Source: Internet
Author: User
Tags image filter image processing library

1. uiimageview does not support Tile)

The uiimageview class fully draws its image and does not call drawrect. to customize the image, you must inherit the uiview.
The uiimageview class is optimized to draw its images to the display. uiimageview will not call drawrect: a subclass. if your subclass needs custom drawing code, it is recommended you use uiview as the base class.

// Use the image as the background of the view to tile the image by view size.

View. backgroundcolor = [uicolor colorwithpatternimage: Image];

2. The images in the resource must be in lower case. The simulators may not be case sensitive, but they are in the real machine.

[Uiimage imagenamed: @ ""]; case sensitive on devices

3. uiview has no background image attribute and background color attribute. You can use addsubview (backgroundimage) to set the background image. We recommend that you set the background color.

4. [uiimage imagenamed: @ ""]; it has the cache feature

This method looks in the system caches for an image object with the specified name and returns that object if it exists. if a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then
Returns the resulting object.

On a device running IOS 4 or later, the behavior is identical if the device's screen has a scale1.0. If the screen has a scale2.0, This method first searches for an image file with the same filename with@2x
Suffix appended to it. For example, if the file's name isbutton, It first searchesbutton@2x. If it finds a 2x, it loads that image and setsscaleProperty of the returnedUIImageObject2.0.
Otherwise, it loads the unmodified filename and setsscaleProperty1.0.

On iOS 4 and later, the name of the file is not required to specify the filename extension. Prior to iOS 4, you must specify the filename extension.

After multiple operations, the application may frequently experience a memory warning, resulting in automatic exit. After positioning, it was found that [uiimage imagenamed: @ ""] The allocated images were not released. The previous information obtained from the official reference should be [uiimage imagenamed: @ ""]. The allocated image system will be placed in the cache. The cache management rules are not clearly described. From this perspective, [uiimage imagenamed:] is only suitable for reading textures in the UI, while some large resource files should try to avoid using this interface.

+ (Uiimage *) imagewithcontentsoffile :( nsstring *)Path this method does not cache the image object.


5. uiimage-(uiimage *) stretchableimagewithleftcapwidth :( nsinteger) leftcapwidth topcapheight :( nsinteger) topcapheight

Specifies the part that is not scaled in two directions.

Corresponding rightcapwidth = image. Size. Width-(image. leftcapwidth + 1 );

Bottingapheight = image. Size. Height-(image. topcapheight + 1 );

A pixel between leftcapwidth and rightcapwidth, topcapheight, and botw.apheight is tiled to achieve scaling.

In addition, uiview. contentstretch does not seem to work here.

Note: When using this function, you must obtain its return value. And that method cocould return nil if the base image is less than 5 pixels wide or 5 pixels tall since it needs the 4 pixels for the caps + 1 pixel to stretch.

Example: Set the custom return button on the navigation bar

-(Void) setnavigationbackbutton
{
Const int kleftwidth = 25;
Uiimage * image = [[uiimage imagenamed: @ "back.png"] stretchableimagewithleftcapwidth: kleftwidth topcapheight: 0];
Uiimage * hightlightedimage = [[uiimage imagenamed: @ "back_pressed.png"] stretchableimagewithleftcapwidth: kleftwidth topcapheight: 0];
Nsstring * text = [[[self. navigationcontroller viewcontrollers] objectatindex: 0] navigationitem]. title;
Uifont * font = [utility fontwithsize: 12];
Cgsize stringsize = [text sizewithfont: font];
Cgfloat textwidth = stringsize. Width + kleftwidth;

Uibutton * button = [uiviewutil createuibuttonwithimage: Image
Hightlightedimage: hightlightedimage
Selectedimage: Nil
Title: Text
Titlecolor: color (0x33,0x33,0x33)
Posx: 0 posy: 0];
Button. titlelabel. font = [utility fontwithsize: 12];
Cgrect rect = button. frame;
Rect. Size. width = textwidth;
Button. Frame = rect;
Uibarbuttonitem * buttonitem = [[uibarbuttonitem alloc] initwithcustomview: button];
[Button addtarget: Self action: @ selector (popviewcontrolleranimated) forcontrolevents: uicontroleventtouchupinside];
[[Self navigationitem] setleftbarbuttonitem: buttonitem];
[Buttonitem release];
[Button release];
}

6. Supports built-in animation to play the image sequence.

Imageview. animationimages = imagearray;
Imageview. animationduration = 0.75f;
[Self. View addsubview: imageview];
[Imageview startanimating];

7. For uiimageview, you must set self. userinteractionenabled = yes to support interaction.

8. Image Construction Methods:

Uigraphicsbeginimagecontext (cgsizemake (side_length, side_length); // create a new image Context
Cgcontextref context = uigraphicsgetcurrentcontext ();
...
Uiimage * theimage = uigraphicsgetimagefromcurrentimagecontext (); // convert the context into a uiimage object.

Uigraphicsendimagecontext ();

Return theimage;

9. Dynamic images, such as GIF, are not supported by the iPhone. You can use uiwebview instead of GIF.

(Void) imagepickercontroller :( uiimagepickercontroller *) picker didfinishpickingmediawithinfo :( nsdictionary *) info

{

Nsstring * referenceurl = [info objectforkey: uiimagepickercontrollerreferenceurl];
Nslog (@ "% @", referenceurl); // assets-Library: // asset/asset. jpg? Id = 1000000001 & ext = JPG

Alassetslibrary * library = [[alassetslibrary alloc] init];

...
}

# Import <assetslibrary/alassetslibrary. h>
# Import <assetslibrary/alassetrepresentation. h>

Http://stackoverflow.com/questions/5187251/ios-select-a-gif-from-the-photo-library-convert-to-nsdata-for-use-in-multipart

10. Save the image to the album.

-(Void) savetophotosalbum
{
Uiimage * image = nil;
Nsobject * OBJ = ..;
If ([OBJ iskindofclass: [uiimage class])
{
Image = (uiimage *) OBJ;
}
Else
{
Image = [uiimage imagewithdata :( nsdata *) OBJ];
}

If (image)
{
Uiimagewritetosavedphotosalbum (image, self, @ selector (image: didfinishsavingwitherror: contextinfo :), nil );
}
Else
{
// @ "Failed to save ";
}
}

# If 1
-(Void) image :( uiimage *) image didfinishsavingwitherror :( nserror *) Error contextinfo :( void *) contextinfo
{
If (error = nil)
// @ "Saved successfully ";
Else
// @ "Failed to save ";
}
# Endif

11. Emoji

Emoji is a one-character set developed in Japan. It is integrated in iOS and can be determined by programming to enable or disable this function.

It has been tested and can be displayed in uilabel, uibutton, and uiwebview, using Unicode codes such as "\ ue415.

Http://pukupi.com/post/1964/

Http://www.easyapns.com/iphone-emoji-alerts

Http://www.emoji-cheat-sheet.com/

Http://en.wikipedia.org/wiki/Emoji

You can use the draw method of nsstring to draw an expression on uiimage:

Cgsize size = [text sizewithfont: afont];
If (uigraphicsbeginimagecontextwitexceptions! = NULL)
{
Uigraphicsbeginimagecontextwitexceptions (size, no, 0.0 );
}
Else
{
Uigraphicsbeginimagecontext (size );
}
[Text drawatpoint: cgpointmake (0.0, 0.0) withfont: afont];
Uiimage * image = uigraphicsgetimagefromcurrentimagecontext ();
Uigraphicsendimagecontext ();
Return image;

 
IOS emoji is enabled for programming. The test is not successful.

Http://blog.csdn.net/favormm/article/details/6774899

Solution to the emoji display box in ios5.1

Emoji cannot be displayed normally on some ios5.1 devices. the reason is that emoji on ios4 uses Softbank encoding. After ios5, emoji is put into unicode6.0, which may cause some incompatibility of the old encoding.
The solution is to use new encoding on ios5 and old encoding on ios4 and below.
Because some ios5.1 can be displayed normally, some may not. According to our test, all of 5.x uses the new encoding, And all of 4.x and below use the old encoding.
Apple's own conversion table: Unicode new encoding on the left of the http://opensource.apple.com/source/ICU/ICU-461.13/icuSources/data/translit/Any_SoftbankSMS.txt, Softbank old encoding on the right, Please convert it yourself

Http://www.cocoachina.com/bbs/read.php? Tid = 96847 & page = 1

12. After taking a picture with uiimagepickercontroller, the image is rotated 90 degrees. This may not happen in 5.0.

# Pragma mark uiimagepickercontrollerdelegate
-(Void) imagepickercontroller :( uiimagepickercontroller *) picker didfinishpickingmediawithinfo :( nsdictionary *) info
{
[Picker dismissmodalviewcontrolleranimated: Yes];

Nsstring * mediatype = [info objectforkey: uiimagepickercontrollermediatype];
If ([mediatype isw.tostring :( nsstring *) kuttypeimage]) // @ "public. Image"
{
Uiimage * image = [info objectforkey: uiimagepickercontrolleroriginalimage];
Uiimageorientation imageorientation = image. imageorientation;
If (imageorientation! = Uiimageorientationup)
{
// The original image can be displayed according to the camera angle, but the uiimage cannot be determined. Therefore, the obtained image will turn left 90 degrees.
// Adjust the image Angle
Uigraphicsbeginimagecontext (image. size );
[Image drawinrect: cgrectmake (0, 0, image. Size. Width, image. Size. Height)];
Iportraitimageview. Image = uigraphicsgetimagefromcurrentimagecontext ();
Uigraphicsendimagecontext ();
// Adjust the image Angle
}
}
}

// You can edit the selected image.

Uiimagepickercontroller * pickercontroller = [[uiimagepickercontroller alloc] init];
Pickercontroller. Delegate = self;
Pickercontroller. allowsediting = yes; // You can edit the selected image.

If (buttonindex = 0)
{
If ([uiimagepickercontroller issourcetypeavailable: uiimagepickercontrollersourcetypephotolibrary])
{
Pickercontroller. sourcetype = uiimagepickercontrollersourcetypephotolibrary;
}
}
Else
{
If ([uiimagepickercontroller issourcetypeavailable: uiimagepickercontrollersourcetypecamera])
{
Pickercontroller. sourcetype = uiimagepickercontrollersourcetypecamera;
}
}

[[Uiviewutil firstavailableuiviewcontrollerwithview: Self] presentmodalviewcontroller: pickercontroller animated: Yes];
[Pickercontroller release];

-(Void) imagepickercontroller :( uiimagepickercontroller *) picker didfinishpickingmediawithinfo :( nsdictionary *) info
{
[Picker dismissmodalviewcontrolleranimated: Yes];

Nsstring * mediatype = [info objectforkey: uiimagepickercontrollermediatype];
If ([mediatype isw.tostring :( nsstring *) kuttypeimage]) // @ "public. Image"
{
Uiimage * image = [info objectforkey: uiimagepickercontrollereditedimage];
Iportraitimageview. Image = image;
}
}

Test again later. Test the result:

Uiimage * image = [info objectforkey: uiimagepickercontrolleroriginalimage];
Uiimageorientation imageorientation = image. imageorientation;

The imageorientation value is shown in the left column. When taking a picture, the Home Key is positioned as the value in the right column.

Under uiimageorientationright
Uiimageorientationup right
Uiimageorientationdown left
Uiimageorientationleft

If a black screen appears after taking the photo multiple times, check whether the printed content is warned, and check whether the previous picture has not been released.

See ....

Https://gist.github.com/1531596

13. For Nil's image. Size. width value, the result may be uncertain. The value may be 0 on most machines, while some machines get an uncertain value.

Therefore, you must determine that if it is nil, the width value is 0.

14. When the image is filled with backgroundcolor, the transparent area on the image may become black. Backgroundcolor is paved with the content size.

 

15.in the Resource Directory, if icon.png 114x114 is present at the same time, icon@2x.png 114x114 pixels

[Image imagenamed: @ "icon.png"]; The size is. The image is icon.png and the pixel/point is.
Nsstring * Path = [[nsbundle mainbundle] pathforresource: @ "icon" oftype: @ "PNG"];
[Uiimage imagewithcontentsoffile: pathcon 1111is. The image used is icon.png, And the pixel/point is.
Path = [[nsbundle mainbundle] pathforresource: @ "icon @ 2x" oftype: @ "PNG"];
[Uiimage imagewithcontentsoffile: The pathpattern size is. The image is icon@2x.png and the pixel/point is.

The results of imagewithcontentsoffile are the same as those in logs.

It looks like the @ 2x recognition in the file name is completed by the uiimage after finding the Image Based on the path, and it looks at the final image name, not necessarily in the path.

[Nsdata datawithcontentsoffile: path] and [uiimage imagewithdata: dataworkflow. For icon.pngand icon@2x.png, the size is 114 points and the pixel/point is.

Images can also be grouped by directory. [[[[nsbundle mainbundle] resourcepath] stringbyappendingpathcomponent: @ "Directory Name"] stringbyappendingpathcomponent: @“img1.png "],

Then use [uiimage imagewithcontentsoffile: path] to load the file.

However, images must be managed using resource folders.

16. rounded Images

Http://www.cocoachina.com/bbs/read.php? Tid = 1757 & page = 1

17. Add a filter to the image.

Use the core graphic API to parse the image into a four-channel rgba bitmap and put it into the memory. Then there is an array in the memory, each of the four elements in the array is the rgba value (0-255) of a pixel on the image. Change the RGB value and write it back to regenerate.

Changing to a black-and-white image is to add the RGB value of each pixel to calculate the average value, and then write it back. For example, r = B = G = 100, which is gray. In the for loop, the RGB values of each pixel are changed to their respective average values, and the photos are black and white. If the image changes to a nostalgic image, the background color is yellow, that is, the ratio of RG is increased, and B remains unchanged, because the Red-Green match is yellow.
With the colormatrix concept in Android, it is important to adjust the RGB value of each pixel to a new value.

Http://www.cocoachina.com/bbs/read.php? Tid = 69525

Http://www.cnblogs.com/leon19870907/articles/1978065.html

18. Draw a translucent rectangle
Cgcontextref CTX = uigraphicsgetcurrentcontext ();
Cgcontextsetfillcolorwithcolor (CTX, [[uicolor blackcolor] colorwithalphacomponent: 0.5]. cgcolor );
Cgcontextaddrect (CTX, cgrectmake (0, 0,320, 44 ));
Cgcontextclosepath (CTX );
Cgcontextdrawpath (CTX, kcgpathfill );

19. gpuimage

Https://github.com/BradLarson/GPUImage

Compile gpuimage. xcodeproj in the framework directory, generate. A, and then compile an application under examples.

20. Take a photo of uiview

The basic principle is to depict the layer of the uiview to the graphic context. # Import "quartzcore/calayer. H"
Uiview global photograph-(uiimage *) imagefromview :( uiview *) view
{
Uiimage * screenimage; uigraphicsbeginimagecontext (view. Frame. size );
[View. layer renderincontext: uigraphicsgetcurrentcontext ()];
Screenimage = uigraphicsgetimagefromcurrentimagecontext ();
Uigraphicsendimagecontext ();
Return screenimage;
} Uiview local photograph-(uiimage *) imagefromview :( uiview *) view rect :( cgrect) rect
{
Cgpoint Pt = rect. origin;
Uiimage * screenimage; uigraphicsbeginimagecontext (rect. size );
Cgcontextref context = uigraphicsgetcurrentcontext ();
Cgcontextconcatctm (context, cgaffinetransformmaketranslation (-(INT) pt. X,-(INT) pt. y ));
[View. layer renderincontext: Context];
Screenimage = uigraphicsgetimagefromcurrentimagecontext ();
Uigraphicsendimagecontext ();
Return screenimage;
}
Reference http://bj007.blog.51cto.com/1701577/533632

Uiimagepickercontrollersourcetypephotolibrary: displays all photos.

Uiimagepickercontrollersourcetypecamera: Indicates selecting a photo from the camera

Uiimagepickercontrollersourcetypesavedphotosalbum: indicates that only the photo is selected from the album.

21. Open-source code for image processing of bradlarson/gpuimage

Gpuimage is a powerful and easy-to-use image processing library. Provides a wide range of image processing filters and supports real-time Filters for cameras and cameras. As the name suggests, gpuimage is GPU-based image acceleration. Therefore, the image processing speed is very fast and the image filter can be customized. Supports arc. [Code4app.com]

22. Use blend in IOS to change the image color

Http://onevcat.com/2013/04/using-blending-in-ios/

Http://blog.csdn.net/ricky1217/article/details/8591827

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.