0

I have a collection view in which the cell we place image from gallery. On a cell there is a button which rotate an image when we click it. Now when I click the image it rotates the image but when I send it to the server it does not goes in the rotated angle as I selected, it goes as it is coming from gallery or camera. When I select an image from gallery it gives this in an array of image ,

 III (
   "<UIImage: 0x608000299b90>, {1000, 624}"
 )

and in path of image array it comes this,

FFF is(
  "/Users/Apple/Library/Developer/CoreSimulator/Devices/23D54E17-36FB-4397-BA5E-09A05EB0EBF3/data/Containers/Data/Application/C1D07D05-11B3-4D49-8D91-884FC3DA1A06/Documents/E7IME.png"
)

Now i want to store the the rotated condition of image and send it to server with same rotated condition. but when i click the button nothing comes in console and just image rotates.

enter image description here

Kerberos
  • 4,036
  • 3
  • 36
  • 55
Raheel
  • 39
  • 6

2 Answers2

0

You have to rotate the image itself and saving that state, try below:

#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)

-(UIImage *)rotateImage:(UIImage*)src byRadian:(CGFloat)radian
{
    CGRect rect = CGRectApplyAffineTransform(CGRectMake(0,0, src.size.width, src.size.height), CGAffineTransformMakeRotation(radian));
    CGSize rotatedSize = rect.size;

    // Create the bitmap context
    UIGraphicsBeginImageContext(rotatedSize);
    CGContextRef bitmap = UIGraphicsGetCurrentContext();

    // Move the origin to the middle of the image so we will rotate and scale around the center.
    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);

    //   // Rotate the image context
    CGContextRotateCTM(bitmap, radian);

    // Now, draw the rotated/scaled image into the context
    CGContextScaleCTM(bitmap, 1.0, -1.0);
    CGContextDrawImage(bitmap, CGRectMake(-src.size.width / 2, -src.size.height / 2, src.size.width, src.size.height), [src CGImage]);

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

Usage:

UIImage *rotatedImage = [self rotateImage:[UIImage imageNamed:@"urImageName"] byRadian:DEGREES_TO_RADIANS(90)];
[self.image setImage:rotatedImage];

Note: you have then to send the rotatedImage.

Answer is inspired from: https://stackoverflow.com/a/20860719/4004429

Ayman Ibrahim
  • 1,359
  • 15
  • 24
  • Bro, how can we judge from it that which image is rotated in the array of images , so that we can send the rotated images to server. @Ayman Ibrahim – Raheel Oct 17 '17 at 09:20
  • when you press in the collection view cells, you only rotate the view not that image itself. So you can get the indexPaths of selected items, self.collectionView.indexPathsForSelectedItems? then traverse the images array and rotate the selected items (using the method I provided above) and send it to the server. – Ayman Ibrahim Oct 17 '17 at 09:40
  • how to traverse the array, this is the point in which i'm stuck , i'm not getting how can i traverse the array after selection all the images in array. @Ayman Ibrahim – Raheel Oct 17 '17 at 09:46
  • Ok, let selectedCellsIndexPaths = self.collectionView.indexPathsForSelectedItems? if let paths = selectedCellsIndexPaths { var rotatedImages = [UIIimage]() for index in paths { let rotatedImage = self.rotate(yourdefaultImageArr[path.row] ) rotatedImages.append(rotatedImage) } } you should after that use 'rotatedImages' to send to the server. note: the code above is explanatory , your code should do something like that. – Ayman Ibrahim Oct 17 '17 at 10:06
  • you should be able to traverse an Array, no one is going to do spooning for you. this 'self.collectionView.indexPathsForSelectedItems?' returns an array of indexPaths you can run a for loop on it and check what indexpath is selected and use this value to get the image from the original array and rotate it and store it in another array to send to server. If you found my answer helpful please mark the answer as accepted. – Ayman Ibrahim Oct 17 '17 at 10:15
0

You can save your modified image (imageView) in the gallery with

UIImageWriteToSavedPhotosAlbum(imageView, nil, nil, nil);

The sequence of nil is the completion block, you can read the usage here.

Kerberos
  • 4,036
  • 3
  • 36
  • 55
  • i have an array of images so how can i know which image is rotated or not so that i could store the rotated conditon in it? @Kerberos – Raheel Oct 17 '17 at 09:18
  • I don't know the specific case but you can save images in another dataSource with the images (modified and not, in the same order of the other) and use it or create a support array with a bool with the modified status. – Kerberos Oct 17 '17 at 09:33